Disclaimer / Clause de non-responsabilité / Haftungsausschluss:
Use of the code examples is at your own risk!
L’utilisation des exemples de code s’effectue à vos propres risques et périls !
Die Nutzung der Codebeispiele erfolgt auf eigenes Risiko!
To run PowerShell scripts safely and conveniently in Windows, you can initiate the launch using a *.cmd file with the "-executionpolicy Bypass" parameter. The following script creates the *.cmd file and a shortcut to it. Shortcuts can optionally be launched as an administrator.
$ScriptVer = "V.1.02 - 10.2025"
Clear-Host
Write-Host "-*-" -ForegroundColor DarkGray
Write-Host "Ξ schremar.com - Run PS1 via CMD - $ScriptVer" -ForegroundColor DarkGray
Write-Host "---"
# Open browse dialog to select PS1 file
Add-Type -AssemblyName System.Windows.Forms
$dialog = New-Object System.Windows.Forms.OpenFileDialog
$dialog.InitialDirectory = Get-Location
$dialog.Filter = "PowerShell Scripts (*.ps1)|*.ps1"
$dialog.Title = "Select PS1 File..."
if ($dialog.ShowDialog() -eq "OK") {
$ps1Path = $dialog.FileName
$ps1Name = [System.IO.Path]::GetFileNameWithoutExtension($ps1Path)
$ps1Dir = [System.IO.Path]::GetDirectoryName($ps1Path)
$cmdPath = Join-Path $ps1Dir "$ps1Name.cmd"
$lnkPath = Join-Path $ps1Dir "$ps1Name.lnk"
# Create CMD
$cmdContent = "@echo off`r`n" + "powershell.exe -executionpolicy Bypass -file `"$ps1Path`""
Set-Content -Path $cmdPath -Value $cmdContent -Encoding ASCII
# Create Shortcut
$shell = New-Object -ComObject WScript.Shell
$shortcut = $shell.CreateShortcut($lnkPath)
$shortcut.TargetPath = $cmdPath
$shortcut.WorkingDirectory = $ps1Dir
$shortcut.IconLocation = "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe,0"
$shortcut.Save()
Clear-Host
Write-Host "-*-" -ForegroundColor DarkGray
Write-Host "Ξ schremar.com - Run PS1 via CMD - $ScriptVer" -ForegroundColor DarkGray
Write-Host "---"
Write-Host -ForegroundColor Cyan "✅ CMD file and shortcut were created in the same folder as the PS1 file:"
Write-Host -ForegroundColor DarkYellow "➡️ CMD`t`t: $cmdPath"
Write-Host -ForegroundColor DarkYellow "➡️ Shortcut`t: $lnkPath"
} else {
Write-Host -ForegroundColor Magenta "❌ No file selected."
}
Write-Host
Write-Host "..."
Pause