PowerShellTraps
PowerShellTraps copied to clipboard
Collection of PowerShell traps and oddities
PowerShellTraps is a collection of some PowerShell traps and oddities shown by demo scripts, workarounds, and automated tests. On invoking scripts change to their directory. See also TESTS. Some scripts require Invoke-PowerShell.ps1.
Index
- Basic
- Class
- Clixml
- Cmdlets
- Module
- PowerShell.exe
Basic
- Operators
-andand-orhave the same precedence - Invoking native apps with error output
- Automatic variables as parameters or local variables
- Break and Continue with not matching label
- Break and Continue without loop
- Collection property enumeration
- Comparison operators work differently with scalars and collections
- Compound assignment operators
- Count-and-Length
- Mixed objects
PSCustomObjectdoes not have surrogateCountandLengthCountandLengthof a scalar fail in the strict mode
- Dictionary Count, Keys, Values may misbehave
- Different kinds of null
- Double quoted strings and sub-expressions with double quotes
- DynamicParam
- Dynamic switch parameter
$MyInvocation.ExpectingInputis false inDynamicParam
- Enums evaluated to true
- Errors of unusual type
- FormatEnumerationLimit
- Function invoked like method
- Invocation with odd paths
- LastExitCode
- Local action preference variables
- Misleading error location
- Missing ternary operator
- Negative number literal argument
- Null converted to empty string
- Number of returned objects
- Operators
-match,-notmatchdo not reset$matches - Counter-intuitive equal precedence of some operators
- Tricky properties of types implementing IDictionary
- Properties of XML nodes
- Provider specific filters
- PSCustomObject
[ref]may be unwrapped unexpectedly- Directive
#requires - Runspace pool memory leaks with
Close() - Statements are not expressions
- ErrorRecord formatting may fail in the strict mode in the default host
- String constructor
- String equality operators
switchis a looping construct- Tempting wrong operators
Throwmay not terminate- ThrowTerminatingError
ThrowTerminatingErrordoes not trigger the immediate catchThrowTerminatingErrorproduces not true terminating errors
- Too simple function names
trapcreates a new scopetrapwithcontinuetrapwith nobreakorcontinuetryandtrapcatch terminating errors- Unexpected output
- Unexpected overloaded method
- Unrolled collections
- v5 method
new() ValidateScriptand cryptic error messages- ValueFromPipeline used for several parameters
- Parameters with ValueFromRemainingArguments
- When
processblock is called WildcardPattern.Escape()does not escape some backticks
Class
- Attribute with later defined type parameter
- Cannot derive a class from another defined later
- Cannot derive from dot sourced
- Identical script blocks issue
- Methods do not see implicitly created variables
- Parser fails on custom type
Clixml
- Cannot write to hidden files
- Hashtable case sensitivity
OrderedDictionarybecomesHashtableScriptBlockbecomesString- No
LiteralPathin v2
Cmdlets
- ConvertFrom-Csv
ConvertFrom-Csvhas undocumented partial comment support
- ConvertFrom-Json
ConvertFrom-Jsonreturns an array not unrolledConvertFrom-Jsonpipeline inputConvertFrom-Jsonlong integers in v6
- ConvertTo-Json
ConvertTo-JsonArray as PSObjectConvertTo-JsonwithoutCompressmay change data
- Copy-Item
Copy-Item-Excludeis ignored withRecurseCopy-Iteminconsistent destination directory
ForEach-Object- Get-ChildItem
Get-ChildItemgets items converted to strings differentlyGet-ChildItemwildcard-path fails in a directory with backticksGet-ChildItem -LiteralPath -Recursegets nothing for a directory with bracketsGet-ChildItem -Recurseand missing-Pathunexpected searchGet-ChildItem -LiteralPath -Recurseignores-Include
- Get-Event
Get-Eventerratic failures in v2-4
- Get-Item
Get-Itemfails to getCert:Get-Itemworks incorrectly in some locations
- Get-ItemPropertyValue
Get-ItemPropertyValueignoresErrorAction
Get-Uniqueoddities- Get-WmiObject
Get-WmiObject- amended WMI data may fail in the strict modeLatest
- Group-Object
Group-Object -AsHashTableand custom expressionsGroup-Objectand properties made by expressions with no values
Import-Csvmay trim leading spaces- Import-Module
Import-Modulenon-terminating error
- Invoke-Expression
Invoke-ExpressionErrorActionis ignored in favour of$ErrorActionPreference
- Invoke-RestMethod
Invoke-RestMethodreturns an array not unrolled
Join-Pathfails if the path drive does not exist- Read-Host
- v5 output before
Read-Host
- v5 output before
- Remove-Item
Remove-Item -LiteralPathfails in a directory with bracketsRemove-Itemmay ignore items specified byExclude
Remove-ItemProperty -Confirmfails on 'No' (Registry)- Rename-Item
Rename-Itemhas noLiteralPathin v2
- Resolve-Path
Resolve-Path -Pathwith wildcards misses hidden items
- Select-Xml
Select-Xml -Contentdoes not support input as stream
- Set-Content
Set-Contentfails in a directory with bracketsSet-Contentunexpected output location
Split-Pathwith UNC paths: mind the current providerStart-Process- Test-Path
Test-PathodditiesTest-Path -Pathwith wildcards misses hidden items
Wait-Processfails if a process name is not foundWhere-ObjectWrite-DebugWrite-Progress
Module
- Conflict with aliases
- Function parent scope
- Not found function
- Not found function (part 2)
- Not found variable
- Script block scope
- How to set a variable in the caller scope
- Strict mode is not propagated
- v2 does not support the manifest field
RootModule
PowerShell.exe
- Exit code 0 with Command syntax error
- Exit code 0 with File script error
- Exit code 5 with File, 1 with Command
- Exit code depends
- Global and script scope
- Not current version
- Different positional parameters in powershell and pwsh
- Switch parameter with value
- Unexpected start location
- v3 interactive issues
- Parameter
Versionmust be the first. Versionand scriptblock host problem