PowerShellTraps icon indicating copy to clipboard operation
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 -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
    • Mixed objects
    • PSCustomObject does not have surrogate Count and Length
    • Count and Length of 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.ExpectingInput is false in DynamicParam
  • 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, -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 constructor
  • String equality operators
  • switch is a looping construct
  • Tempting wrong operators
  • Throw may not terminate
  • ThrowTerminatingError
    • ThrowTerminatingError does not trigger the immediate catch
    • ThrowTerminatingError produces not true terminating errors
  • Too simple function names
  • trap creates a new scope
  • trap with continue
  • trap with no break or continue
  • try and trap 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

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
  • OrderedDictionary becomes Hashtable
  • ScriptBlock becomes String
  • No LiteralPath in v2

Cmdlets

  • ConvertFrom-Csv
    • ConvertFrom-Csv has undocumented partial comment support
  • ConvertFrom-Json
    • ConvertFrom-Json returns an array not unrolled
    • ConvertFrom-Json pipeline input
    • ConvertFrom-Json long integers in v6
  • ConvertTo-Json
    • ConvertTo-Json Array as PSObject
    • ConvertTo-Json without Compress may change data
  • Copy-Item
    • Copy-Item - Exclude is ignored with Recurse
    • Copy-Item inconsistent destination directory
  • ForEach-Object
  • Get-ChildItem
    • Get-ChildItem gets items converted to strings differently
    • Get-ChildItem wildcard-path fails in a directory with backticks
    • Get-ChildItem -LiteralPath -Recurse gets nothing for a directory with brackets
    • Get-ChildItem -Recurse and missing -Path unexpected search
    • Get-ChildItem -LiteralPath -Recurse ignores -Include
  • Get-Event
    • Get-Event erratic failures in v2-4
  • Get-Item
    • Get-Item fails to get Cert:
    • Get-Item works incorrectly in some locations
  • Get-ItemPropertyValue
    • Get-ItemPropertyValue ignores ErrorAction
  • Get-Unique oddities
  • Get-WmiObject
    • Get-WmiObject - amended WMI data may fail in the strict mode Latest
  • Group-Object
    • Group-Object -AsHashTable and custom expressions
    • Group-Object and properties made by expressions with no values
  • Import-Csv may trim leading spaces
  • Import-Module
    • Import-Module non-terminating error
  • Invoke-Expression
    • Invoke-Expression ErrorAction is ignored in favour of $ErrorActionPreference
  • Invoke-RestMethod
    • Invoke-RestMethod returns an array not unrolled
  • Join-Path fails if the path drive does not exist
  • Read-Host
    • v5 output before Read-Host
  • Remove-Item
    • Remove-Item -LiteralPath fails in a directory with brackets
    • Remove-Item may ignore items specified by Exclude
  • Remove-ItemProperty -Confirm fails on 'No' (Registry)
  • Rename-Item
    • Rename-Item has no LiteralPath in v2
  • Resolve-Path
    • Resolve-Path -Path with wildcards misses hidden items
  • Select-Xml
    • Select-Xml -Content does not support input as stream
  • Set-Content
    • Set-Content fails in a directory with brackets
    • Set-Content unexpected output location
  • Split-Path with UNC paths: mind the current provider
  • Start-Process
  • Test-Path
    • Test-Path oddities
    • Test-Path -Path with wildcards misses hidden items
  • Wait-Process fails if a process name is not found
  • Where-Object
  • Write-Debug
  • Write-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 Version must be the first.
  • Version and scriptblock host problem