Run Powershell 7 from CMD - powershell

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?

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 ".

Related

Run Powershell with arguments via CMD

I have the following .BAT script that basically calls Powershell and it works fine.
CMD /C PowerShell "SL -PSPath '%CD%'; $Path = (GL).Path; SL ~; Start PowerShell -Verb RunAs -Args \"-ExecutionPolicy ByPass" SL -PSPath '"$Path"'; & '".\RDPSwitchToConsle.ps1"'"\""
I'm trying to extend it to add more arguments, specifically -WindowStyle Hidden.
I've tried a couple of things, but can't get the script .BAT to run. It just opens Powershell straight away and closes immediately, which is a sign that the arguments/parameters are not loaded correctly due to incorrect structure.
I've tried adding the argument like this:
CMD /C PowerShell "SL -PSPath '%CD%'; $Path = (GL).Path; SL ~; Start PowerShell -Verb RunAs -Args \"-ExecutionPolicy ByPass -WindowStyle Hidden" SL -PSPath '"$Path"'; & '".\RDPSwitchToConsle.ps1"'"\""
Apply -WindowStyle Hidden to the Start-Process (start) call, not to the nested powershell.exe call.
Here's a simplified version of your command:
PowerShell "SL ~; Start -WindowStyle Hidden -Verb RunAs PowerShell \"-ExecutionPolicy ByPass SL -PSPath '%CD%'; ^& .\RDPSwitchToConsle.ps1\""

How do I run a PowerShell script as administrator using a shortcut?

I'm trying to run a PowerShell script as administrator using a shortcut. I have tried many ways, but it still does not work:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoExit -Verb RunAs Start-Process powershell.exe -ArgumentList '-file C:\project\test.ps1'
With this command, it will create two PowerShell windows and one window will close.
I also tried this one:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoExit Start-Process powershell.exe -Verb RunAs -File 'C:\project\test.ps1'
Can some one please help?
Tl;dr
This will do the trick:
powershell.exe -Command "& {$wd = Get-Location; Start-Process powershell.exe -Verb RunAs -ArgumentList \"-ExecutionPolicy ByPass -NoExit -Command Set-Location $wd; C:\project\test.ps1\"}"
Explanation
First, you have to call PowerShell to be able to execute Start-Process. You don't need any additional paramters at this point, because you just use this first PowerShell to launch another one. You do it like this:
powershell.exe -Command "& {...}"
Inside the curly braces you can insert any script block. First you will retrieve your current working directory (CWD) to set it in the new launched PowerShell. Then you call PowerShell with Start-Process and add the -Verb RunAs parameter to elevate it:
$wd = Get-Location; Start-Process powershell.exe -Verb RunAs -ArgumentList ...
Then you need to add all desired PowerShell parameters to the ArgumentList. In your case, these will be:
-ExecutionPolicy ByPass -NoExit -Command ...
Finally, you pass the commands that you want to execute to the -Command parameter. Basically, you want to call your script file. But before doing so, you will set your CWD to the previously retrieved directory and THEN call your script:
Set-Location $wd; C:\project\test.ps1
In total:
powershell.exe -Command "& {$wd = Get-Location; Start-Process powershell.exe -Verb RunAs -ArgumentList \"-ExecutionPolicy ByPass -NoExit -Command Set-Location $wd; C:\project\test.ps1\"}"

Run powershell commands from cmd / batch

I would like to run the following as administrator:
PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command
.\Get-WindowsAutoPilotInfo.ps1 -ComputerName $env:computername
-OutputFile .\computers.csv -append
I would like to simply double click on a .cmd or .bat file and have it invoke the Powershell script as administrator. Here's what I have:
PowerShell "SL -PSPath '%CD%'; $Path = (GL).Path; SL ~; Start
PowerShell -Verb RunAs -Args \"-ExecutionPolicy Unrestricted -Noexit"
SL -PSPath '"$Path"'; & '".\UninstallBloatware.ps1" "-ComputerName
$env:computername" "-OutputFile .\computers.csv" "-append"' "\""
I copied most of the code above from somewhere I can't remember. I don't know enough about quotes structure to know how to fix this. Any ideas what I'm doing wrong?

Run a powershell script on a remote system with elevated permissions to enable remoting

I am trying to use the following code to copy a PowerShell script to remote windows 7 machine; run it with elevated privileges on this machine to enable remoting on that system.
It is copying the script file to the remote system but it is not executing the command in the remote PowerShell session because of the empty $command variable (the second line in the script below is not working).
Copy-Item -Path C:\users\user1\Myscript.ps1 -Destination \\some-computer\c$\Myscript.ps1
$command = PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\Myscript.ps1""' -Verb RunAs > C:\PS-result1.txt}"
$cmd = "CMD.EXE /c "+$command
Invoke-WmiMethod -class Win32_process -name Create -ArgumentList $cmd -ComputerName "some-computer"
Start-Sleep -s 8
Get-Content \\some-computer\C$\PS-result1.txt
Is it possible to accomplish this?
Thanks,
Using WMI to call CMD to call PowerShell to call Start-Process to call PowerShell again? That seems a little complicated.
Try something much simpler:
$command = "PowerShell.exe ""C:\Myscript.ps1"" > ""C:\PS-result1.txt"""
Invoke-WmiMethod -class Win32_process -name Create -ArgumentList $command -ComputerName "some-computer"

Starting an admin shell then executing multiple commands

I have a powershell script and a bat file that launches it. I want the bat file to open powershell, then have powershell start another shell with elevated privileges, then run two commands. First command is change directory, second command is start a powershell script.
So far I have this:
powershell -NoProfile -ExecutionPolicy ByPass -Command "& {Start-Process PowerShell -Verb RunAs -ArgumentList '-NoExit -NoProfile -ExecutionPolicy Bypass cd %~dp0 .\App\Deploy-Application.ps1}'"
This is the section I'm having problems with:
cd %~dp0 .\App\Deploy-Application.ps1
I want to run these two commands but I'm not sure how. It runs a single command. I tried adding a semicolon between the commands but it didn't work.
Made a quick test and this is what i got working:
Test.bat
cd %~dp0
powershell -NoProfile -Command ".\test.ps1"
Test.ps1
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "-noprofile & '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
Write-Host "Rawr"
Pause
If i run the batch file, it opens the powershell script that then checks if the current window is being run as an administrator and if not, reopens the script as an administrator.
After which it displays Rawr on my screen.
In your case instead of the Write-Host you could put
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "-noprofile & '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
cd <Your directory to change to here>
<run command here>
Pause