The PowerShell notebook provides a rich interactive experience through its host. The following are some examples.
Blue
, and you can see the output is rendered in blue afterwards:$host.UI.RawUI.ForegroundColor = [System.ConsoleColor]::Blue
$PSVersionTable
Name Value ---- ----- PSVersion 7.0.0 PSEdition Core GitCommitId 7.0.0 OS Darwin 19.4.0 Darwin Kernel Version 19.4.0: Wed Mar 4 22:28:40 PST… Platform Unix PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…} PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 WSManStackVersion 3.0
Write-Host "Something to think about ..." -ForegroundColor Blue -BackgroundColor Gray
Something to think about ...
Write-Warning "Warning"
Write-Verbose "Verbose" -Verbose
Write-Debug "Debug" -Debug
WARNING: Warning VERBOSE: Verbose DEBUG: Debug
Write-Host -NoNewline
as expected:Write-Host "Hello " -NoNewline -ForegroundColor Red
Write-Host "World!" -ForegroundColor Blue
Hello World!
$cred = Get-Credential
"$($cred.UserName), password received!"
PowerShell credential request Enter your credentials. User: Joe Password for user Joe: ········ Joe, password received!
Write-Verbose "Ask for name" -Verbose
$name = Read-Host -Prompt "What's your name? "
Write-Host "Greetings, $name!" -ForegroundColor DarkBlue
VERBOSE: Ask for name What's your name? : Joe Greetings, Joe!
Read-Host -Prompt "token? " -AsSecureString
token? : ········
System.Security.SecureString
Get-Command nonExist -ErrorAction Inquire
Confirm The term 'nonExist' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. [Y] Yes [A] Yes to All [H] Halt Command [S] Suspend [?] Help(default is 'Y')Select: Y Get-Command: The term 'nonExist' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Write-Output | ForEach-Object { "I received '$_'" }
cmdlet Write-Output at command pipeline position 1 Supply values for the following parameters: InputObject: Something to output I received 'Something to output'
Get-Process | select -First 5 | % { start-sleep -Milliseconds 300; $_ }
NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName ------ ----- ----- ------ -- -- ----------- 0 0.00 0.00 0.00 0 …39 0 0.00 0.00 0.00 1 1 0 0.00 0.00 0.00 109 109 0 0.00 0.00 0.00 110 110 0 0.00 0.00 0.00 113 113
## Demo the progress bar
For ($i=0; $i -le 100; $i++) {
Write-Progress -Id 1 -Activity "Parent work progress" -Status "Current Count: $i" -PercentComplete $i -CurrentOperation "Counting ..."
For ($j=0; $j -le 10; $j++) {
Start-Sleep -Milliseconds 5
Write-Progress -Parent 1 -Id 2 -Activity "Child work progress" -Status "Current Count: $j" -PercentComplete ($j*10) -CurrentOperation "Working ..."
}
if ($i -eq 50) {
Write-Verbose "working hard!!!" -Verbose
"Something to output"
}
}
VERBOSE: working hard!!! Something to output