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.
- Operators
-and
and-or
have 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
- Dictionary Count, Keys, Values may misbehave
- Different kinds of null
- Double quoted strings and sub-expressions with double quotes
- DynamicParam
- Enums evaluated to true
- Errors of unusual type
- FormatEnumerationLimit
- Function invoked like method
- Invocation with odd paths
- LastExitCode
- Local action preference variables
- Magic method
Where
- Misleading error location
- Missing ternary operator
- Negative number literal argument
- Null converted to empty string
- Number of returned objects
- Operators
-match
,-notmatch
do 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 as Boolean
- String constructor
- String equality operators
switch
is a looping construct- Tempting wrong operators
Throw
may not terminate- ThrowTerminatingError
- Too simple function names
trap
creates a new scopetrap
withcontinue
trap
with nobreak
orcontinue
try
andtrap
catch terminating errors- Unexpected output
- Unexpected overloaded method
- Unrolled collections
- v5 method
new()
ValidateScript
and cryptic error messages- ValueFromPipeline used for several parameters
- Parameters with ValueFromRemainingArguments
- When
process
block is called WildcardPattern.Escape()
does not escape some backticks
- 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
- Cannot write to hidden files
- Hashtable case sensitivity
OrderedDictionary
becomesHashtable
ScriptBlock
becomesString
- No
LiteralPath
in v2
- ConvertFrom-Csv
- ConvertFrom-Json
- ConvertTo-Json
- Copy-Item
ForEach-Object
- Get-ChildItem
Get-ChildItem
gets items converted to strings differentlyGet-ChildItem
wildcard-path fails in a directory with backticksGet-ChildItem -LiteralPath -Recurse
gets nothing for a directory with bracketsGet-ChildItem -Recurse
and missing-Path
unexpected searchGet-ChildItem -LiteralPath -Recurse
ignores-Include
- Get-Content
- Get-Event
- Get-Item
- Get-ItemPropertyValue
Get-Unique
oddities- Get-WmiObject
- Group-Object
Import-Csv
may trim leading spaces- Import-Module
- Invoke-Expression
- Invoke-RestMethod
- Join-Path
- Read-Host
- Remove-Item
Remove-ItemProperty -Confirm
fails on 'No' (Registry)- Rename-Item
- Resolve-Path
- Select-Xml
- Set-Content
Split-Path
with UNC paths: mind the current providerStart-Process
- Test-Path
Wait-Process
fails if a process name is not foundWhere-Object
Write-Debug
Write-Progress
- 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
- 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
Version
must be the first. Version
and scriptblock host problem