PowerShell
Online PowerShell
main.ps1 runs in PowerShell 7.6 on .NET 10 for Linux, the cross-platform pwsh, not Windows PowerShell 5.1. Read-Host waits for you to type in the terminal while the script runs.
Try it aboveHow PowerShell runs here
- Versions
- PowerShell 7.6 on .NET 10
- Run
pwsh -File main.ps1- Limits per run
- 512 MB memory, 15 s of CPU time on half a core, 2 MB of output
- Time
- Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
- Sandbox
- Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.
A PowerShell script that reads input
This main.ps1 asks for a few scores, then uses the pipeline to summarise them. Read-Host adds the colon and space after its prompt itself:
$count = [int](Read-Host 'How many scores') $scores = for ($i = 1; $i -le $count; $i++) { [double](Read-Host "Score $i") } $stats = $scores | Measure-Object -Average -Maximum -Minimum "Average: {0:N1}" -f $stats.Average "Best: $($stats.Maximum), worst: $($stats.Minimum)" $scores | Where-Object { $_ -ge $stats.Average } | ForEach-Object { "Above average: $_" }
$ pwsh -File main.ps1 How many scores: 3 Score 1: 72 Score 2: 88 Score 3: 95 Average: 85.0 Best: 95, worst: 72 Above average: 88 Above average: 95
Reading input in PowerShell
- Read-Host always returns a string. Cast it, [int](Read-Host 'Age') or [double](...), before doing arithmetic, or "2" + 3 gives 23.
- A string on its own line is written to the output, so "Average: {0:N1}" -f $x prints without Write-Output.
- Arguments from Run settings arrive in $args.
What isn’t available here
- Windows-only cmdlets and modules: no registry, WMI/CIM, Active Directory or Out-GridView. The script runs on Linux.
- Installing modules from the PowerShell Gallery: there’s no network during a run.
Errors you’ll see
- “The term 'Write-Outptu' is not recognized as a name of a cmdlet, function, script file, or executable program.” A misspelt command.
- “Cannot convert value "abc" to type "System.Int32".” A cast such as [int] got text that isn’t a number.
- Most errors don’t stop the script: PowerShell prints them and carries on, so the return code can still be 0. Put $ErrorActionPreference = 'Stop' at the top to stop at the first one.
Questions
Is this PowerShell 7 or Windows PowerShell?
PowerShell 7.6 (pwsh) on .NET 10, running on Linux. Scripts written for Windows PowerShell 5.1 work unless they use Windows-only cmdlets.
How do I read input in PowerShell?
$name = Read-Host 'Name' shows the prompt and waits for you to type in the terminal. Cast the answer with [int] or [double] for numbers.
Why is my return code 0 after an error?
PowerShell errors are non-terminating by default. Set $ErrorActionPreference = 'Stop' or use throw to end the script with an error.