4

I've written a powershell script that takes a bool parameter I also have a windows shortcut to this powershell script. The problem is that whenever I try to run the script from the shortcut, the parameter is interpreted as a string, not a bool, and the script crashes.

This is what I initially placed under the target section of my shortcut:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Program Files\MySoftware\diagnostic\DiagnosticTool.ps1" $true

I've searched for solutions online and tried the following options as well:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Program Files\MySoftware\diagnostic\DiagnosticTool.ps1" -copyAll:$true

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Program Files\MySoftware\diagnostic\DiagnosticTool.ps1" -copyAll:`$$true

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Program Files\MySoftware\diagnostic\DiagnosticTool.ps1" "`$$true"

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Program Files\MySoftware\diagnostic\DiagnosticTool.ps1" `$$true

And multiple such variations.

This is my question to you: Is there a way to send the value of a bool parameter to the script when running a from a windows shortcut?

1 Answer 1

5

try with this:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "& 'C:\Program Files\MySoftware\diagnostic\DiagnosticTool.ps1'" -copyAll:$true

of with -file parameter to pass bool to a switch parameter of your script try:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file "C:\Program Files\MySoftware\diagnostic\DiagnosticTool.ps1" {-All:$False}

The last one is taked from TechNet but to be honest I'll never be able to work with it.

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

3 Comments

sorry, but the space in Program files messes with Command it just tries to run the script c:\Program and thinks the rest of the path is a parameter, while for your second approach I've got the following error: Missing expression after unary operator '-'.At line:1 char:137 + C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Program Files\MySoftware\diagnostic\DiagnosticTool.ps1" {- <<<< All:$true} + CategoryInfo : ParserError: (-:String) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MissingExpressionAfterOperator
that works. Thanks very much, though I don't really know what & does in this particular case, but thanks.
@memoryofadream & is the 'Call operator', it lets you run commands that are stored in variables and represented by strings or enclosed in '/" as a string

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.