6

Suppose I'm running a PowerShell script that takes several input parameters. The command looks like:

psScript.ps1 -arg1 "arg1value" -arg2 "arg2value"

Is there a way to store this exact command in a variable within the script so that I can log it?

Specifically, I'd like to know what to assign to the variable $currentCommand:

$currentCommand = <something>
Write-Host "currently running script " $currentCommand

Such that the Write-Host output would be the exact command line used to invoke the script. If the script command was the same as above, for example, then the output would be:

currently running script psScript.ps1 -arg1 "arg1value" -arg2 "arg2value"

2 Answers 2

8

This may suit your needs:

Write-Host "currently running script " $myinvocation.Line

Reference

Sign up to request clarification or add additional context in comments.

1 Comment

This does not work with calls to other functions. For example I am trying to get the current command as part of an error handler function, and $myInvocation in this case gives me the details about the call to the error handler.
3

The $MyInvocation variable will have the information. Here is a good blog post about it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.