How to Install RSAT in Windows 11 Using PowerShell

Install RSAT in Windows 11 using PowerShell

To install RSAT in Windows 11 using PowerShell, open PowerShell as administrator and run Add-WindowsCapability with the name of the tool you need. RSAT is built into Windows 11 as optional features, so there’s nothing to download: That line adds the Active Directory tools, including the ActiveDirectory PowerShell module. Below I cover the checks to … Read more

How to Convert Seconds to Hours and Minutes in PowerShell

Convert seconds to hours and minutes in PowerShell

To convert seconds to hours and minutes in PowerShell, turn the number into a TimeSpan with New-TimeSpan -Seconds. Its Hours, Minutes and Seconds properties hold the parts, and ToString formats it as hh:mm:ss: Output: The backslashes in ‘hh\:mm\:ss’ are required, because a colon means something special in TimeSpan formats. Every example ran in PowerShell 7.6 … Read more

How to Find a Character in a String Using PowerShell

Find a character in a string with PowerShell

To find a character in a string with PowerShell, use Contains() to check whether it’s there, or IndexOf() to get its position. IndexOf counts from 0 and returns -1 when the character isn’t found: Output: Below I cover the operator versions, case sensitivity, the second or nth occurrence, every position and counting matches. Every example … Read more

How to Move a File to a Folder in PowerShell

Move a file to a folder in PowerShell

To move a file to a folder in PowerShell, use Move-Item with the file in -Path and the folder in -Destination. The file keeps its name and disappears from the original folder: Output: That’s the whole job when everything is where you expect. The rest of this guide covers the cases that go wrong. I … Read more

How to Create a GUID in PowerShell (Formats, Validation and Hex)

Create a GUID in PowerShell

To create a GUID in PowerShell, run the New-Guid cmdlet. It returns a new, random globally unique identifier every time: Output (yours will be different): Below I cover the other way to create one, the text formats, validation and converting a GUID to hex. Every example ran in PowerShell 7.6 and Windows PowerShell 5.1, with … Read more

How to Remove the Last Line from a File Using PowerShell

Remove the last line from a file with PowerShell

To remove the last line from a file with PowerShell, read the lines with Get-Content, drop the last one with Select-Object -SkipLast 1, and write the rest back with Set-Content: Output: Reading into a variable first matters, as the next section shows. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, with the … Read more

How to Convert a String to a Hashtable in PowerShell (and Back)

Convert a string to a hashtable in PowerShell

To convert a string to a hashtable in PowerShell, use ConvertFrom-StringData. It reads text with one key=value pair per line and returns a hashtable: Output: Below I handle one-line strings, backslashes, JSON and value types, then go the other way and turn a hashtable back into a string. Every example ran in PowerShell 7.6 and … Read more

How to Create a Folder with the Current Date Using PowerShell

Create a folder with the current date in PowerShell

To create a folder with the current date in PowerShell, format today’s date as text and pass it to New-Item with -ItemType Directory: Output: I used a fixed date so the output doesn’t change. In a real script, use $today = Get-Date. Every example ran in PowerShell 7.6 and Windows PowerShell 5.1, with the same … Read more