Alexander Martin
Alexander Martin

Reputation: 483

Get literal command line of PowerShell script

For reasons involving privilege elevation, I need to be able to invoke a script in exactly the same way as it was initially run. For this reason, I want to be able to retrieve the literal, complete, unprocessed command line used to originally invoke a script. Optimally, this would behave like:

# DoIt.ps1
Get-CommandLine | Write-Output

PS> ./DoIt.ps1 -Something something abc 123
./DoIt.ps1 -Something something abc 123

C:\> PowerShell.exe ./DoIt.ps1 -Something something abc 123
./DoIt.ps1 -Something something abc 123

But I can deal with keeping PowerShell.exe, and obviously it doesn't have to be a single cmdlet.

Upvotes: 1

Views: 492

Answers (1)

Alexander Martin
Alexander Martin

Reputation: 483

As advised by Mathias and TheMadTechnician, I found that $MyInvocation.Line provides exactly what I'm looking for. Don't know why Google didn't help, but I found the answer.

Upvotes: 2

Related Questions