PowerShell

Replace operator gotcha

Using an arbitrary string for the replacement value in -replace can lead to unexpected results. The problem lies with unexpected replacement markers inside the replacement string, like $_, $1, etc. To avoid this, the $ character inside the replacement string must be escaped. Another solution is to simply use string's Replace(), which does literal replacement.

Find commands by parameter

Find commands with a specific parameter type:

Get-Command -ParameterType [datetime]

See available help info on a specific command:

Get-Help ls | fl synopsis, description, syntax, parameters  # use `gm` for more

Laptop battery info

Query interesting battery info, including estimated run time (in minutes):

Get-CimInstance win32_battery | fl estimated*

Pipeline Cleanup

about Functions Advanced Methods - PowerShell Microsoft Learn https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_methods?view=powershell-7.3#clean 13/01/2023 15:05

> The `clean` block was added in PowerShell 7.

Add new Dispose {} type block so script cmdlets have opportunity to cleanup · Issue #6673 · PowerShell_PowerShell · GitHub https://github.com/PowerShell/PowerShell/issues/6673 13/01/2023 15:05

The issue report that led to the introduction of the `clean` block.

Powershell Finally block skipped with Ctrl-C - Stack Overflow https://stackoverflow.com/questions/45104509/powershell-finally-block-skipped-with-ctrl-c 13/01/2023 15:12

https://stackoverflow.com/questions/28522507/powershell-function-dispose-or-abort-handler

Types and Accelerators

All .Net Exceptions List https://powershellexplained.com/2017-04-07-all-dotnet-exception-list/ 19/04/2023 23:28

How to Discover Types_Classes in PowerShell - Spiceworks https://community.spiceworks.com/how_to/153476-how-to-discover-types-classes-in-powershell 19/04/2023 23:23

NET types - PowerShell Team https://devblogs.microsoft.com/powershell/net-types/ 19/04/2023 21:32

Nivot Ink The ramblings of code enthusiast and PowerShell MVP, Oisin Grehan https://web.archive.org/web/20160316204338/http://nivot.org/blog/ 19/04/2023 23:20

PowerTip Use PowerShell to List Exported Types - Scripting Blog https://devblogs.microsoft.com/scripting/powertip-use-powershell-to-list-exported-types/ 19/04/2023 23:25

Understanding PowerShell Data Types and Accelerators https://adamtheautomator.com/powershell-data-types/ 19/04/2023 23:26

Where can I find a list of Powershell .NET Type Accelerators - Stack Overflow https://stackoverflow.com/questions/1145312/where-can-i-find-a-list-of-powershell-net-type-accelerators 19/04/2023 20:17

from: https://stackoverflow.com/a/1429572

Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.Information]::TypeName($d.item(1).getfolder)

Misc

Can you set an object's DefaultDisplayPropertySet in a PowerShell v2 script - Stack Overflow https://stackoverflow.com/questions/1369542/can-you-set-an-objects-defaultdisplaypropertyset-in-a-powershell-v2-script/1891215#1891215 03/05/2023 23:54

https://duckduckgo.com/?q=PSStandardMembers&ia=web

    https://learn.microsoft.com/en-us/powershell/scripting/developer/cmdlet/defining-default-member-sets-for-objects?view=powershell-7.3

    https://devblogs.microsoft.com/powershell/psstandardmembers-the-stealth-property/

https://duckduckgo.com/?q=powershell+memberset&ia=web

    https://learn.microsoft.com/en-us/powershell/scripting/developer/ets/membersets?view=powershell-7.3

        property sets / member sets (...)

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_intrinsic_members?view=powershell-7.3

        psbase, psadapted, psextended, psobject, pstypenames
        > Using psobject is similar to using Get-Member, but provides more flexibility. For example, you can enumerate the properties of an object and their values.
        .ForEach() / .Where()
        .Count / .Length
        .New()

how to create psobject with hidden properties https://social.technet.microsoft.com/forums/windowsserver/en-US/fbc304a4-78d7-4046-abca-2220f4c9181d/how-to-create-psobject-with-hidden-properties 03/05/2023 23:54

Parameter Attribute Declaration - PowerShell Microsoft Learn https://learn.microsoft.com/en-us/powershell/scripting/developer/cmdlet/parameter-attribute-declaration?view=powershell-7.2 04/05/2023 17:08

$input type in advanced functions https://github.com/PowerShell/PowerShell/issues/1563

GitHub - nightroman/PowerShellTraps: Collection of PowerShell traps and oddities https://github.com/nightroman/PowerShellTraps