How to Create a Character Array in PowerShell

Create a character array in PowerShell

To create a character array in PowerShell, call ToCharArray() on a string. It returns a Char[] array with one item for each character, which you can loop through, filter or count: Output: Below I compare the other ways to build one, then cover letter ranges, filtering, counting and joining characters back into a string. Every … Read more

How to Replace Values in an Array in PowerShell

Replace values in an array in PowerShell

To replace a value in a PowerShell array, assign to its index: $array[1] = ‘new value’. To replace text in every item at once, use -replace on the whole array: Output: Index assignment changes the array in place. The rest of this guide covers replacing across all items, replacing whole items only, and a few … Read more

PowerShell Where-Object for Null or Empty Values

PowerShell Where-Object for null or empty values

To find null or empty values with Where-Object, test the property with [string]::IsNullOrWhiteSpace(). It catches $null, empty strings and values that are only spaces. Here it finds employees with no department in a CSV file: Output: Dan’s department is two spaces, so it looks filled in but isn’t. To keep only the rows that have … Read more

PowerShell Where-Object with Multiple Conditions

PowerShell Where-Object with multiple conditions

To use multiple conditions with PowerShell Where-Object, put them in one script block and join them with -and or -or: -and needs every condition to be true, and -or needs at least one. Below I combine them with parentheses, use -in, avoid a common CSV trap and compare the simplified syntax and the .Where() method. … Read more

How to Write a String to a File in PowerShell

Write a string to a file in PowerShell

To write a string to a file in PowerShell, use Set-Content. It creates the file, or replaces whatever is in it. To add more text to the end, use Add-Content: Output: Those two cmdlets cover most jobs. Below I’ll show how to write variables, multiple lines and arrays, how to append, and the encoding differences … Read more

How to Count Lines in a File in PowerShell

Count lines in a file in PowerShell

To count the lines in a file with PowerShell, read it with Get-Content and check .Count. Get-Content returns one item per line, so the count is the number of lines: Output: That’s the right answer for most files. But the popular Measure-Object -Line method gives a different number, and neither is the fastest for big … Read more

How to Check if a File Contains a String in PowerShell

Check if a file contains a string in PowerShell

To check if a file contains a string in PowerShell, run Select-String with -Quiet. It returns True or False, so it fits straight into an if statement: Output: The match ignores case, and the pattern is a regular expression. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1 on a sample Logs folder, … Read more

How to List File Names Only in PowerShell

List file names only in PowerShell

To list file names only in PowerShell, run Get-ChildItem with -File and -Name. You get plain names, without folders, sizes or dates: Below I use a small Reports folder with two subfolders to show every variation: recursive lists, names without extensions, filters and saving the list to a file. List names with Get-ChildItem -Name -Name … Read more

How to Get File Details and File Version in PowerShell

Get details of a file in PowerShell

To get the details of a file in PowerShell, use Get-Item with the file path. It returns the name, size, dates and attributes, and Format-List shows them one per line: Output: Length is the size in bytes. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, and they returned the same results. Read … Read more