Similar Posts

Subscribe
Notify of
4 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Erzesel
7 months ago

PowershellScripte in autostart are run with the default action for .ps1 files and this is “edit in the editor” . Ergo opens the file in the Notepad.

Without changing the default procedure in the registry, or a link does not work.

An elegant alternative would be the Powershell script as a batch. to disguise.

Powershell hybrid batch.cmd

<# : Batch Abschnitt
start "" powershell -c "&([Scriptblock]::Create((gc '%~f0' -enc UTF8 -raw )))"
exit /b
: Ende Batch Abschnitt #>

#hier  Dein Powershellcode:
Write-Host Hallo Welt -fo green
Write-Host Γεια σου Κόσμο  -fo mag
Write-Host Viele Grüße aus Powershell -fo red
pause

I’ve been using this hybrid technique for years without problems. even with complex Powershellscripts

DeskClock.cmd

<# : Batch Abschnitt ,Batch sieht in dieser Zeile die Eingabe aus der Datei # an ein SprungLabel, Powershell sieht den begin eines Kommentars. 
:: etwas Tricky um Consolenfenster nur  so kurz wie Möglich sichtbar zu lassen
:: erstmal minimierten Start von Powershell erzwingen ( bis Powershell den Parameter -WindowStyle Hidden ausführen kann)
:: akzeptiert UTF8 gespeicherte Scripte
start "" /min powershell -WindowStyle hidden "&([Scriptblock]::Create((gc '%~f0' -enc UTF8 -raw )))"
exit /b
: Ende Batch und Powershellkommentar #>

using namespace System.Windows.Forms
using namespace System.Drawing

$WindowTitle='DesktopUhr'
#Wenn es bereits läuft  raus
$Mut = New-Object System.Threading.Mutex($True, $WindowTitle)
if (!$Mut.WaitOne(0)) {
    $Mut.Close();
    (New-Object -c Wscript.Shell).Popup("$WindowTitle is running",2,"",0)
    exit
}
Add-Type -a 'System.Windows.Forms'

$DisplayFont = [Font]::new("Consolas",32,[FontStyle]3)
$TimeDisplayFormat='HH:mm:ss'

$ClockDisplay = New-Object 'Label'
$ClockDisplay.Font = $DisplayFont
$ClockDisplay.Text = (Get-Date).ToString($TimeDisplayFormat)
$ClockDisplay.AutoSize = $True
$ClockDisplay.ForeColor = '255,0,0'

$Form = New-Object 'Form'
$Form.Text = $WindowTitle 
$Form.FormBorderStyle = 0 #Rahmen aus
$Form.TopMost = $True
$Form.StartPosition = 'manual'
$Form.Location = '30,30'
$Form.BackColor = '0,0,0' #schwarz
$Form.TransparencyKey = $Form.BackColor #kein Hintergrund
$Form.Controls.Add($ClockDisplay)
$Form.Size = $Form.PreferredSize


$Timer1 = New-Object 'Timer'
$Timer1.Enabled = $True
$Timer1.Interval = 100
$Timer1_Action={
    $ClockDisplay.Text = (Get-Date).ToString($TimeDisplayFormat) 
}
$Timer1.add_Tick($Timer1_Action)
[Application]::Run($Form)
$Mut.ReleaseMutex(); 
$Mut.Dispose();


Rowal
6 months ago
Reply to  Erzesel

This is brilliant and a solution in all cases where the option is offered to run a batch file but no Powershell file.

mjutu
7 months ago

Yes, I do. The execution of scripts must be allowed.