Sending multiple arguments to a Powershell opened with Start-Process - powershell

So I'm using the command
Start-Process powershell -Verb runas -ArgumentList $cmds
where $cmds is
$cmds = "cd C:\", "dir"
I just want the new powershell that I'm opening to run multiple commands before it automatically closes.

Change $cmds to:
$cmds = {"cd C:\"; "dir";}

Related

Powershell scriptopen a window and send command to window

I'm trying to open a CMD as system using PowerShell and then send a command to that system window.
what i've got so far is this:
$opensystempromt = '"/c PsExec.exe -is cmd.exe'
[System.Diagnostics.Process]::Start('cmd.exe',$opensystempromt)
$systemcommand = 'CustomerService.exe deploy aa.txt bb.txt'
what i can't figure out is how to send the system command to the newly opened CMD.
and there's no documentation for it anywhere, that i could find
I'd use Start-Process to open cmd.
Start-Process -FilePath "C:\Windows\System32\cmd.exe" -verb runas -ArgumentList {/k Insert your variables here}
with -verb runas you'll open cmd as an administrator. ArgumentList is your "command part". /k leaves the cmd open, after the commands are done. If you want cmd to close instead, use /c.

Start-Process with auto elevated permissions and passing command inline

I need to start a windows service on the local computer through PS by directly running the PS script w.o the need to manually elevate the permissions. This code works for me:
Start-Process powershell -Verb runas -ArgumentList "-file MyFileName.ps1"
Where MyFileName.ps1 contains:
Start-Service MyServiceName
But I want to keep it simple and instead of storing the command into a separate file, I want to run a single script. The following does not work for me:
Start-Process powershell -Verb runas -ArgumentList "-command '& {Start-Service MyServiceName}'"
What am I missing?
Start-Process invokes a new process. The invocation doesn't recognize single quotes as quoting characters, so instead of passing a parameter -command with a command string '&{Start-Service MyServiceName}' you're passing 4 tokens: -command, '&, {Start-Service, and MyServiceName}.
Change this:
"-command '& {Start-Service MyServiceName}'"
into this:
"-command `"& {Start-Service MyServiceName}`""

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

PowerShell execute external command in same window

I am trying to execute an external command using powershell, without having the second program to popup, I need to execute this program within the same PowerShell window and output both the log and the errors.
I started with this:
$outcome = Start-Process -Wait -FilePath "cmd.exe" -ArgumentList "dir" -NoNewWindow 2>&1
$outcome
But it doesn't work as expected. I still see the new window popping up with DOS and no redirect at all about the output, the errors and so on.
Am I doing something wrong?
Does this work for you?
$outcome = Invoke-Expression "cmd.exe /c dir"
$outcome
My suggestion is to use the call operator
& cmd.exe /c dir
You can just run it. You're missing "/c".
$outcome = cmd /c dir
With the added complication of start-process, you'd have to save the output to a file.
Start-Process -Wait -FilePath "cmd.exe" -ArgumentList "/c","dir" -NoNewWindow 2>&1 -RedirectStandardOutput cmd.log

Send a command to a powershell opened with "Start-Process powershell -Verb runas"

I want to open an admin powershell and send it a command (eventually a script). Right now, it doesn't matter what command, but I've tried things like:
Start-Process powershell -Verb runas < $something
or
$something | Start-process powershell -Verb runas
just to get some text to show up in the new admin powershell window. Any ideas?
That awkward moment when you do a little more research and find what you want: this link will help anyone: how to execute set of commands in elevated mode of powershell
Essentially, add the -argument argument to your command
Start-Process powershell -verb runas -argument dir