Close

Call Powershell script from command file

Just another gotcha. I have a command file Kw1c_Build.cmd that calls a Powershell script Kw1c_Build.ps1. The command file has the following code:

@Echo Off

Echo Changing PowerShell Execution policy…
powershell Get-ExecutionPolicy
powershell Set-ExecutionPolicy Unrestricted

powershell %~dpn0.ps1

pause

What’s important to know, is that %~dpn0 returns the Drive, Path and Name of the currently executing script. So, if your .cmd file and .ps1 file have the same name, you can use a call like that. In my opinion you can also reference the ps1 file by it’s path or even use a relative path: powershell .\Kw1c_Build.ps1.
I’m not sure about the last assumption, but must be working.