'Run Powershell 7 from CMD
I have a .bat script that runs Powershell as Admin and then runs a Powershell script in the same folder as the .bat file. This works perfectly fine:
CMD /C powershell "Set-Location -PSPath '%CD%'; $Path = (Get-Location).Path; Set-Location ~; Start powershell -Verb RunAs -Args "-ExecutionPolicy ByPass" Set-Location -PSPath '"$Path"'; & '".\Start_TOW_VM.ps1"'"""
I am now trying to use Powershell 7 (pwsh) instead and I thought it'd be as simple as changing to this:
CMD /C pwsh "Set-Location -PSPath '%CD%'; $Path = (Get-Location).Path; Set-Location ~; Start pwsh -Verb RunAs -Args "-ExecutionPolicy ByPass" Set-Location -PSPath '"$Path"'; & '".\Start_TOW_VM.ps1"'"""
Unfortunately, it doesn't work and complains about the Set-Location command, even though that command works perfectly fine in Powershell 7. What am I doing wrong here?
Solution 1:[1]
Figured it out, here's the solution:
CMD /C pwsh -c Set-Location -PSPath '%CD%'; $Path = (Get-Location).Path; Set-Location ~;write-host $path; start pwsh -Verb RunAs "-command Set-ExecutionPolicy ByPass; Set-Location -PSPath '"$Path"'; & '".\Start_TOW_VM.ps1"'"
Simply needed to add -command inside the escaped ".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | shadowz1337 |
