I have some PowerShell scripts that accept many long parameters, like,
myScript.ps1 -completePathToFile "C:\...\...\...\file.txt" -completePathForOutput "C:\...\...\...\output.log" -recipients ("[email protected]") -etc.
I can't seem to make PowerShell run such scripts unless all the parameters are on a single line. Is there a way to invoke the script more like this?
myScript.ps1
-completePathToFile "C:\...\...\...\file.txt"
-completePathForOutput "C:\...\...\...\output.log"
-recipients (
"[email protected]",
"[email protected]"
)
-etc
The lack of readability is driving me nuts, but the scripts really do need to be this parametric.
Running a PowerShell script from the Command Prompt If you would like to run a PowerShell script in CMD, you'll need to execute it by calling the PowerShell process with the -File parameter, as shown below: PowerShell -File C:\TEMP\MyNotepadScript. ps1. PowerShell -File C:\TEMP\MyNotepadScript.
Before you can run a script on Windows, you need to change the default PowerShell execution policy. Execution policy does not apply to PowerShell running on non-Windows platforms. The default execution policy, Restricted , prevents all scripts from running, including scripts that you write on the local computer.
The PowerShell scripting language operates by executing a series of PowerShell commands (or a single one), with each command appearing on a separate line. For the text file to be treated as a PowerShell script, its filename needs to end in . PS1 to connote a PowerShell extension.
By default, PowerShell's execution policy is set to Restricted; this means that scripts will not run. You can verify the execution policy setting by using the Get-ExecutionPolicy PowerShell command as shown below. You can change the PowerShell script execution behavior using "Set-ExecutionPolicy".
PowerShell thinks the command is complete at the end of the line unless it sees certain characters like a pipe, open paren or open curly. Just put a line continuation character `` ` at the end of each line but make sure there are no spaces after that continuation character:
myScript.ps1 `
-completePathToFile "C:\...\...\...\file.txt" `
-completePathForOutput "C:\...\...\...\output.log" `
-recipients (
"[email protected]", `
"[email protected]" `
)
If you're on PowerShell 2.0 you can also put those parameters in a hashtable and use splatting e.g:
$parms = @{
CompletePathToFile = 'C:\...\...\...\file.txt'
CompletPathForOutput = 'C:\...\...\...\output.log'
Recipients = '[email protected]','[email protected]'
}
myScript.ps1 @parms
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With