PowerShell: convert a SecureString to a String
A small PowerShell function that uses Marshal.SecureStringToBSTR to turn a SecureString read from the console into plain text for an external program.
Tag
14 related posts.
A small PowerShell function that uses Marshal.SecureStringToBSTR to turn a SecureString read from the console into plain text for an external program.
How to write a PSTypeConverter and register it with a ps1xml file so PowerShell's Extended Type System converts a custom .NET type into DirectoryInfo.
Using .NET's System.IO.Path.IsPathRooted from PowerShell to detect whether a path parameter is relative and join it with the current directory.
A tour of handy PowerShell automatic variables like $?, $Error, $LastExitCode, $MyInvocation, $PSBoundParameters and $PSScriptRoot, and their gotchas.
An empty pipeline result and a plain $null both look like $null in PowerShell, but wrapping them in @() gives arrays of different lengths.
Using Test-Path with the variable: drive to check whether a script-scoped PowerShell variable exists before using it under Set-StrictMode.
Returning an empty array from a PowerShell function gives you $null unless you use the unary comma trick in the return statement.
Typing PowerShell variables and parameters, like [array] for Get-ChildItem results or DirectoryInfo for paths, avoids surprising behavior.
PowerShell switch parameters aren't booleans but SwitchParameter objects, so check them with IsPresent or ToBool(), especially in Pester tests.
Comparing two identical arrays with -eq in PowerShell doesn't return $true, because the operator filters collections instead of comparing them.
A short pipeline script that generates a .psd1 manifest for every PowerShell module in a folder tree, plus a one-liner to unload modules by company.
How Set-StrictMode -Version 2 catches typos and C#-style function calls in PowerShell scripts, and how typed parameters bring it closer to TypeScript.
Import-Module raises a non-terminating error when the module is missing, so the script keeps going; setting $ErrorActionPreference to Stop fixes it.
Invoke-WebRequest downloads in PowerShell can be several times slower because of the progress bar; setting $ProgressPreference to SilentlyContinue fixes it.