It all depends on the script you wish to run and how it was coded.
Use "Get-Help command-or-script-name" and if it's properly written it should display the input it accepts.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
How can I run a PowerShell command that is normally interactive non-interactively, i.e. supplying parameters directly in a PowerShell script instead of letting the user supply them?
What I have in mind is something like "piping" in bash. I'm not sure if anything that's similar to that exists in PowerShell.
It all depends on the script you wish to run and how it was coded.
Use "Get-Help command-or-script-name" and if it's properly written it should display the input it accepts.
Hi @Denis Rutten ,
You can create a script block within your PowerShell script that accepts parameters:
$MyProgram = "powershell.exe"
$MyArguments = "notepad; Start-Sleep 5"
# Create script block with parameters
$ScriptBlock = {
param(
[string]$Program,
[string]$Arguments
)
Start-Process $Program -ArgumentList $Arguments
}
# Run the script block
& $ScriptBlock $MyAppName $MyArguments
You can also use function if more complex solution is required.
As far as piping goes, it is actually a core functionality of PowerShell, here is an example:
# Get-Process gives you all currently running processes
# Piping the output to Where-Object and filtering to processes with the name "Explorer"
# Piping the output to Select-Object and selects given property
Get-Process | Where-Object {$_.Name -EQ "Explorer"} | Select-Object CPU
Another example:
$Names = "John", "Kelly", "Steven"
$Names | ForEach-Object {"Hello $_!"}
I hope this helps.