- https://github.com/PowerShell/PowerShell/commit/3237d43b268a9632881ef879e57c57901a4fcb97
- Add ResponseHeadersVariable Parameter to Invoke-RestMethod
- v6.0.0-beta.8
- v6.0.0: Jan 20, 2018
- https://github.com/PowerShell/PowerShell/commit/04a1fc3b74b0cf4a6d9d2ff8f0d6ada527e0f845
- Allow Web Cmdlets to Ignore HTTP Error Statuses
- SkipHttpErrorCheck
- Add -ResponseStatusVariable to Invoke-RestMethod
- v7.0.0-preview.6
- v7.0.0: Mar 4, 2020
- Allow Web Cmdlets to Ignore HTTP Error Statuses
- ConvertFrom-SecureString -AsPlainText
- added on 7.0
- Windows powershell. E.g.:
[Net.NetworkCredential]::new('', $SecureString).Password
-
Experimental feature PSNativeCommandPreserveBytePipe is now mainstream. PowerShell now preserves the byte-stream data when redirecting the stdout stream of a native command to a file or when piping byte-stream data to the stdin stream of a native command.
- Added on 7.4
-
-
Save jikuja/89372a5479ac06278ca9b9966939dbd8 to your computer and use it in GitHub Desktop.
Automatic enumeration on Process:
| Type | Example | Enumerated? | Notes |
|---|---|---|---|
| System.Collections.IEnumerable | ✅ Yes | ||
Object[] (array) |
@(1,2,3) |
✅ Yes | Each element (1, 2, 3) passes separately. |
List<T> |
[System.Collections.Generic.List[int]]@(1,2,3) |
✅ Yes | Same behavior as arrays. |
Collection<T> |
New-Object System.Collections.ObjectModel.Collection[int] |
✅ Yes | Each item is passed individually. |
Hashtable |
@{A=1;B=2} |
✅ Yes | Enumerates as DictionaryEntry objects. |
String[] |
"a","b","c" |
✅ Yes | Each string passed separately. |
No enumeration on Process:
| Type | Example | Reason |
|---|---|---|
System.String |
"hello" |
Strings are IEnumerable<char> but treated as atomic (single object). |
System.Management.Automation.PSObject |
Any PowerShell-wrapped object | PSObject is a wrapper, not a collection. |
System.Data.DataTable |
$table |
Although it contains rows, treated as one object. |
System.Xml.XmlDocument |
[xml]'<root><a/></root>' |
Represents a whole document, not a collection. |
When you pipe multiple objects to a command, PowerShell sends the objects to the command one at a time. When you use a command parameter, the objects are sent as a single array object. This minor difference has significant consequences.
The parameter accepts values that match the expected .NET type or that can be converted to that type.
Accepts values of the same type expected by the parameter or that can be converted to the type that the parameter is expecting.
The parameter accepts input only when the input object has a property of the same name as the parameter.
Accepts values of the same type expected by the parameter but must also be of the same name as the parameter accepting pipeline input.
PowerShell introduced module autoloading in version 3. To take advantage of this feature, the script module must be saved in a folder with the same base name as the .psm1 file. That folder must be located in one of the directories specified in the $env:PSModulePath environment variable.
Every module should include a module manifest, which is a .psd1 file containing metadata about the module.
The Invoke-Build module is a new alternative to PSake, it is well documented and frequently updated
Valid values of $ConfirmPreference:
- None: PowerShell doesn't prompt automatically. To request confirmation of a particular command, use the Confirm parameter of the cmdlet or function.
- Low: PowerShell prompts for confirmation before running cmdlets or functions with a low, medium, or high risk.
- Medium: PowerShell prompts for confirmation before running cmdlets or functions with a medium, or high risk.
- High: PowerShell prompts for confirmation before running cmdlets or functions with a high risk.
To override the $ConfirmPreference for a single command, use a cmdlet's or function's Confirm parameter.
- To request confirmation, use -Confirm. => $ConfirmPreference = 'Low'
- To suppress confirmation, use -Confirm:$false. => $ConfirmPreference = 'None'
ConfirmImpact does not propagate from CmdLet to CmdLets being called
| cmdlet | ConfirmImpact |
|---|---|
| New-AzADGroup | Medium |
$ConfirmPreference = 'Medium' ; New-AzADGroup <...>
# => prompts Confirmation
$ConfirmPreference = 'Low' ; New-AzADGroup <...>
# => prompts Confirmation
$ConfirmPreference = 'High' ; New-AzADGroup <...>
=> No confirmation
$ConfirmPreference = 'None' ; New-AzADGroup <...>
=> No confirmation
| $ConfirmPreference = 'High'; ` | |
| ` | |
| $DebugPreference = 'SilentlyContinue'; ` | |
| $VerbosePreference = 'SilentlyContinue'; ` | |
| $InformationPreference = 'SilentlyContinue'; ` | |
| $WarningPreference = 'Continue'; ` | |
| $ErrorActionPreference = 'Continue'; ` | |
| ` | |
| $WhatIfPreference = $false |
| function Test-ShouldProcess { | |
| [CmdletBinding( | |
| SupportsShouldProcess, | |
| ConfirmImpact = 'Medium' | |
| )] | |
| param( | |
| [Switch]$Force | |
| ) | |
| if ($Force -and -not $Confirm){ | |
| # confirmation is still asked if called with `-force -debug` | |
| # to disable confirmation `-force -debug -confirm:$false` must be used | |
| Write-Host "Changing `$ConfirmPreference" | |
| $ConfirmPreference = 'None' | |
| } | |
| Show-EnvironmentInformation -Force -PSBoundParameters_ $PSBoundParameters -MyInvocation_ $MyInvocation | |
| # The call to $PSCmdlet.ShouldProcess($file.name) checks for the -WhatIf (and -Confirm parameter) then handles it accordingly. | |
| # Logic is: https://github.com/PowerShell/PowerShell/blob/69e9b439fd38177ff7f3af1ef62f5617454bcf23/src/System.Management.Automation/engine/MshCommandRuntime.cs#L2974-L3000 | |
| if ($PSCmdlet.ShouldProcess('TARGET', 'ShouldProcess')){ | |
| Write-Host "ShouldProcess" | |
| } | |
| <# | |
| /// <summary> | |
| /// Confirm an operation or grouping of operations with the user. | |
| /// This differs from ShouldProcess in that it is not affected by | |
| /// preference settings or command-line parameters, | |
| /// it always does the query. | |
| /// This variant only offers Yes/No, not YesToAll/NoToAll. | |
| /// </summary> | |
| /// <remarks> | |
| /// Cmdlets using ShouldContinue should also offer a "bool Force" | |
| /// parameter which bypasses the calls to ShouldContinue | |
| /// and ShouldProcess. | |
| /// If this is not done, it will be difficult to use the Cmdlet | |
| /// from scripts and non-interactive hosts. | |
| /// ... | |
| /// </remarks> | |
| #> | |
| if ($Force -or $PSCmdlet.ShouldContinue('TARGET','ShouldContinue')) { | |
| Write-Host "ShouldContinue" | |
| } | |
| <# | |
| /// <remarks> | |
| /// Use WriteDebug to display debug information on the inner workings | |
| /// of your Cmdlet. By default, debug output will | |
| /// not be displayed, although this can be configured with the | |
| /// DebugPreference shell variable or the -Debug command-line option. | |
| /// </remarks> | |
| /// <remarks> | |
| /// Use WriteVerbose to display more detailed information about | |
| /// the activity of your Cmdlet. By default, verbose output will | |
| /// not be displayed, although this can be configured with the | |
| /// VerbosePreference shell variable | |
| /// or the -Verbose and -Debug command-line options. | |
| /// </remarks> | |
| /// <remarks> | |
| /// Use WriteWarning to display warnings about | |
| /// the activity of your Cmdlet. By default, warning output will | |
| /// be displayed, although this can be configured with the | |
| /// WarningPreference shell variable | |
| /// or the -Verbose and -Debug command-line options. | |
| /// </remarks> | |
| #> | |
| Write-Host "Write(s) with differenct levels" | |
| Write-Debug "Debug" | |
| Write-Verbose "Verbose" | |
| Write-Information "Information" | |
| Write-Warning "Warning" | |
| Write-Error "Error" | |
| } | |
| <# | |
| > $ConfirmPreference="Low" ; Test-ShouldProcess -force -confirm | |
| Asks confirmation for ShouldProcess | |
| > $ConfirmPreference="High" ; Test-ShouldProcess | |
| Asks confirmation for both | |
| > $ConfirmPreference="High" ; Test-ShouldProcess -Force | |
| No confirmation asked | |
| > $ConfirmPreference="High" ; Test-ShouldProcess -Force -Confirm | |
| Asks confirmation for ShouldProcess | |
| If both -Confirm and -Force are used Confirmation for ShouldProcess are requested. | |
| Still logic of `if ($Force -and -not $Confirm){` does not make any sense and will not work on strict mode | |
| #> | |
| function Show-EnvironmentInformation { | |
| [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess' ,"")] | |
| [CmdletBinding(ConfirmImpact = "None", SupportsShouldProcess = $true)] | |
| param( | |
| [switch] | |
| $Force, | |
| $PSBoundParameters_ = $PSBoundParameters, | |
| $MyInvocation_ = $MyInvocation | |
| ) | |
| function Get-ValueFromPSBoundParameters { | |
| param( | |
| [Parameter(Mandatory=$true)] | |
| [string] | |
| $ParameterName, | |
| $PSBoundParameters_ | |
| ) | |
| $PSBoundParameters_[$ParameterName] ? $PSBoundParameters_[$ParameterName].IsPresent: "NULL(N/A)" | |
| } | |
| if ($Force -and -not $PSBoundParameters.ContainsKey('Confirm')) { | |
| Write-Verbose "Changing `$ConfirmPreference to None" | |
| $ConfirmPreference = 'None' | |
| } | |
| if ($Force -or $Env:PrintRuntimeInformation) { | |
| Write-Host "Confirm:" | |
| Write-Host "ConfirmImpact: $($MyInvocation_.MyCommand.ScriptBlock.Attributes | Where-Object { $_ -is [System.Management.Automation.CmdletBindingAttribute] } | Select-Object -ExpandProperty ConfirmImpact)" | |
| Write-Host "`$Confirm: $(Get-ValueFromPSBoundParameters -PSBoundParameters_ $PSBoundParameters_ -ParameterName "Confirm")" | |
| Write-Host "`$ConfirmPreference: $ConfirmPreference" | |
| Write-Host "Prefrerences:" | |
| Write-Host "`$Debug: $(Get-ValueFromPSBoundParameters -PSBoundParameters_ $PSBoundParameters_ -ParameterName "Debug")" | |
| Write-Host "`$DebugPreference: $DebugPreference" | |
| Write-Host "`$Verbose: $(Get-ValueFromPSBoundParameters -PSBoundParameters_ $PSBoundParameters_ -ParameterName "Verbose")" | |
| Write-Host "`$VerbosePreference: $VerbosePreference" | |
| Write-Host "`$Information: $(Get-ValueFromPSBoundParameters -PSBoundParameters_ $PSBoundParameters_ -ParameterName "Information")" | |
| Write-Host "`$InformationPreference: $InformationPreference" | |
| Write-Host "`$Warning: $(Get-ValueFromPSBoundParameters -PSBoundParameters_ $PSBoundParameters_ -ParameterName "Warning")" | |
| Write-Host "`$WarningPreference: $WarningPreference" | |
| Write-Host "`$Error: $(Get-ValueFromPSBoundParameters -PSBoundParameters_ $PSBoundParameters_ -ParameterName "Error")" | |
| Write-Host "`$ErrorPreference: $ErrorPreference" | |
| Write-Host "`$WhatIf: $(Get-ValueFromPSBoundParameters -PSBoundParameters_ $PSBoundParameters_ -ParameterName "WhatIf")" | |
| Write-Host "`$WhatIfPreference: $WhatIfPreference" | |
| Write-Host "Parameters:" | |
| Write-Host "`$PSBoundParameters.key: $($PSBoundParameters_.keys)" | |
| } | |
| } |
-
- Operations with ConfirmImpact equal to or greater than $ConfirmPreference are confirmed
IF ConfirmImpact >= ConfirmPreference THEN prompt- Also:
- Operations with ConfirmImpact.None are never confirmed
- no operations are confirmed when $ConfirmPreference is ConfirmImpact.None (except when explicitly requested with -Confirm).
-
- When the value of the $ConfirmPreference variable is less than or equal to the risk assigned to a cmdlet or function, PowerShell automatically prompts you for confirmation before running the cmdlet or function.
IF ConfirmPreference <= ConfirmImpact THEN promptIF ConfirmImpact > ConfirmPreference THEN prompt
-
- The call to the ShouldProcess method displays a confirmation prompt only when the ConfirmImpact argument is equal to or greater than the value of the $ConfirmPreference preference variable.
IF ConfirmImpact >= ConfirmPreference THEN prompt
-
- If Confirm parameter is not used, the ShouldProcess() call requests confirmation if the $ConfirmPreference preference variable is equal to or greater than the ConfirmImpact setting of the cmdlet or provider.
IF ConfirmPreference >= ConfirmImpact THEN promptIF ConfirmImpact < ConfirmPreference THEN prompt
-
IF ((threshold == ConfirmImpact.None) || (threshold > cmdletConfirmImpact)) THEN autoconfirm- =>
IF ConfirmPreference <= ConfirmImpact THEN prompt IF ConfirmImpact > ConfirmPreference THEN prompt
| function Test-ShouldProcess { | |
| [CmdletBinding( | |
| SupportsShouldProcess, | |
| ConfirmImpact = 'High' | |
| )] | |
| param( | |
| [Switch]$Force | |
| ) | |
| Write-Verbose "`$PSCmdLet: $PSCmdLet" | |
| Write-Verbose "`$DebugPreference: $DebugPreference" | |
| Write-Verbose "`$ConfirmPreference: $ConfirmPreference" | |
| Write-Verbose "`$Confirm: $Confirm" | |
| Write-Verbose "`$Confirm: $($null -eq $Confirm ? "NULL": "NONNULL")" | |
| if ($Force -and -not $Confirm){ | |
| Write-Verbose "Changing `$ConfirmPreference" | |
| $ConfirmPreference = 'None' | |
| } else { | |
| Write-Verbose "XXXX" | |
| } | |
| Write-Verbose "`$DebugPreference: $DebugPreference" | |
| Write-Verbose "`$ConfirmPreference: $ConfirmPreference" | |
| Write-Verbose "`$Confirm: $Confirm" | |
| Write-Verbose "`$Confirm: $($null -eq $Confirm ? "NULL": "NONNULL")" | |
| # Does this ignore $ConfirmPreference? | |
| # The call to $PSCmdlet.ShouldProcess($file.name) checks for the -WhatIf (and -Confirm parameter) then handles it accordingly. | |
| if ($PSCmdlet.ShouldProcess('TARGET')){ | |
| Write-Output "Some Action" | |
| } | |
| if ($Force -or $PSCmdlet.ShouldContinue('TARGET','OPERATION')) { | |
| Write-Output "Some Action" | |
| } | |
| } | |
| <# | |
| > $ConfirmPreference="Low" ; Test-ShouldProcess -force -confirm | |
| Asks confirmation for ShouldProcess | |
| > $ConfirmPreference="High" ; Test-ShouldProcess | |
| Asks confirmation for both | |
| > $ConfirmPreference="High" ; Test-ShouldProcess -Force | |
| No confirmation asked | |
| > $ConfirmPreference="High" ; Test-ShouldProcess -Force -Confirm | |
| Asks confirmation for ShouldProcess | |
| If both -Confirm and -Force are used Confirmation for ShouldProcess are requested. | |
| Still logic of `if ($Force -and -not $Confirm){` does not make any sense and will not work on strict mode | |
| #> |
Strict mode off:
Set-StrictMode -OffStrict mode latest:
Set-StrictMode -Version 'Latest'