How to Rename Files with a Date in PowerShell

Rename files with a date in PowerShell using Rename-Item

To rename files with a date in PowerShell, pipe the files to Rename-Item and build each new name in a script block from BaseName, a formatted date and Extension: Swap Get-Date for $_.LastWriteTime to stamp each file with its own date. Below I’ll walk through both, plus the duplicate-name, colon and Windows PowerShell 5.1 traps … Read more

How to Extract the Directory from a File Path in PowerShell

Extract the directory from a file path in PowerShell with Split-Path

To extract the directory from a file path in PowerShell, pass the path to Split-Path. Its default -Parent switch drops the file name and returns the folder that holds it: Below I’ll show the folder name on its own, parent folders, Get-Item, the .NET method, relative and network paths, the running script’s folder, and where … Read more

How to Copy and Rename Files in PowerShell

Copy and rename files in PowerShell with Copy-Item

To copy and rename a file in PowerShell, you don’t need two commands. Give Copy-Item the new file name as the -Destination, and it copies and renames in one step: The original stays where it was. Below I’ll cover overwrites, missing folders, bulk renames, dates, counters and the spots where PowerShell 7 and Windows PowerShell … Read more

How to Check if a String Contains Only Numbers in PowerShell

Check if a string contains only numbers in PowerShell

To check if a string contains only numbers in PowerShell, match it against an anchored regex. This returns True only when every character is a digit from 0 to 9: I use [0-9] instead of \d on purpose. Below are the traps, with real output from PowerShell 7 and 5.1. Check if a string contains … Read more

How to Get the File Modified Date in PowerShell (and Change It)

Get a file's modified date in PowerShell with Get-Item and LastWriteTime

A file’s modified date lives in its LastWriteTime property. Grab the file with Get-Item and read that property: That’s the same value File Explorer shows in the Date modified column. CreationTime and LastAccessTime sit right next to it on the same object. In this tutorial, I’ll show you how to get the file modified date … 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 Check if a String Contains Special Characters in PowerShell

Check if a string contains special characters in PowerShell

Need to check if a string contains special characters in PowerShell? Use -match with a negated character class. It returns True as soon as one character that isn’t a letter or digit shows up: The catch is deciding what “special” means. Spaces, underscores and accented letters like é all change the answer, and the -like … Read more

How to Join and Concatenate Strings in PowerShell (-join, +, -f)

Join and concatenate strings in PowerShell with the -join operator

To join strings in PowerShell, put the -join operator between a list and the separator you want. For two or three values, double quotes or the + operator concatenate them just as well, and -f builds a string from a template: Output: Those four cover nearly every case. The details below matter when the list … Read more