How to Convert a String to a Script Block in PowerShell

Convert a string to a script block in PowerShell

To convert a string to a script block in PowerShell, use [scriptblock]::Create(). Then run it with the call operator &: It’s useful when the code comes from a config file or is built from settings, like a filter. Below I cover parameters, the quoting trap, filters, syntax checks and the security risk you need to … Read more

How to Convert a Hex String to a Byte Array in PowerShell

Convert a hex string to a byte array in PowerShell

To convert a hex string to a byte array in PowerShell 7, call [System.Convert]::FromHexString(). Windows PowerShell 5.1 doesn’t have that method, so there you convert each pair of hex digits with [Convert]::ToByte($pair, 16): Below I show both, then a function that handles spaces, dashes and 0x prefixes, checks the input, and turns the bytes back … Read more

How to Find Invalid Characters in a String Using PowerShell

Find invalid characters in a string using PowerShell

To find invalid characters in a string in PowerShell, compare it against the characters Windows doesn’t allow. For file names, IndexOfAny with GetInvalidFileNameChars returns the position of the first bad one, or -1 if the name is clean: “Invalid” depends on where the text is going, though. Below I check file names, list every bad … Read more

How to Find a Carriage Return in a String in PowerShell

Find a carriage return in a string in PowerShell

To find a carriage return in a string in PowerShell, look for the escape sequence “`r”. Contains() tells you whether it’s there, and IndexOf() tells you where: Carriage returns are invisible, so they cause some of the most confusing bugs, like two strings that look the same but don’t match. Below I make them visible, … Read more

How to Replace a String Before a Character in PowerShell

Replace or remove everything before a character in a PowerShell string

To replace a string before a character in PowerShell, use the -replace operator with a regex that matches from the start of the string up to that character: That pattern stops at the first colon. Below I’ll cover the last-occurrence version, keeping the delimiter, a no-regex option with IndexOf(), -split, special characters, file renames, and … Read more

How to Check if a String Starts With Text in PowerShell

Check if a string starts with text in PowerShell with StartsWith, -like and -match

The quickest PowerShell string starts with test is the StartsWith() method, which returns True or False: StartsWith() is case-sensitive, so I’ll also show the case-insensitive options, the -like and -match operators, and two traps where PowerShell 7 and Windows PowerShell 5.1 give different answers. Check if a string starts with text using StartsWith() The method … Read more

Cannot Convert Value to Type System.String in PowerShell (Fixed)

Fix for the PowerShell error Cannot convert value to type System.String with the hidden array cases tested

PowerShell throws “Cannot convert value to type System.String” when an advanced function’s [string] parameter receives an array instead of one value. Call it once per item, or join the items into one string: In this tutorial, I’ll show you how to reproduce cannot convert value to type system.string, find the hidden array behind it, and … Read more

PowerShell Convert String to URI: Cast, Encode and Validate

PowerShell convert string to URI examples

To convert a string to a URI in PowerShell, cast it with the [uri] type accelerator. You get a System.Uri object with the scheme, host, path and query already split out: That works the same in PowerShell 7 and Windows PowerShell 5.1. The catch is in the details: the cast doesn’t really validate, and URL … Read more

How to Add a New Line in a PowerShell String

Add a new line in a PowerShell string

To add a new line in a PowerShell string, put `n (backtick n) inside a double-quoted string. The text after it starts on the next line: Output: That covers most scripts. Below I show the other new line options, when each one matters, and how to turn spaces into new lines. I ran every example … Read more