PowerShell foreach File in a Folder (With Examples)

PowerShell foreach file in a folder

To loop through each file in a folder in PowerShell, get the files with Get-ChildItem -File and process them in a foreach loop: Each $file is a full file object with Name, Length, LastWriteTime and more. Below I use a sample Invoices folder to show the pipeline version, filtering, subfolders, counters, renaming and parallel processing. … Read more

Delete User Profiles Older Than 30 Days with PowerShell

Delete user profiles older than 30 days using PowerShell

To delete user profiles older than 30 days with PowerShell, find them with Win32_UserProfile, skip system and loaded profiles, and remove each one with Remove-CimInstance. Run it as administrator, with -WhatIf first: Remove-CimInstance deletes the profile folder and its registry entry together. Below I explain each filter, test it on sample data, and cover remote … Read more

How to Convert Files to Base64 in PowerShell

Convert files to Base64 in PowerShell

To convert a file to Base64 in PowerShell, read its bytes with [System.IO.File]::ReadAllBytes and pass them to [System.Convert]::ToBase64String: It works the same in PowerShell 7 and 5.1. Below I encode and decode a sample file, fix the -Encoding Byte error, stream large files and compare certutil. Encode a file to Base64 Here I encode a … Read more

Windows Terminal vs PowerShell: What’s the Difference?

Windows Terminal vs PowerShell

In Windows Terminal vs PowerShell, the difference is simple: Windows Terminal is the window, and PowerShell is the shell that runs inside it. Windows Terminal draws the text, tabs and panes, while PowerShell understands your commands and runs scripts. So it’s not really Windows Terminal or PowerShell. You run PowerShell in Windows Terminal, or in … Read more

How to Check Who Modified a File Last with PowerShell

Check who modified a file last in Windows with PowerShell

Windows doesn’t record who last modified a file. NTFS keeps the time of the last change, but not the user, so to check who modified a file with PowerShell you need file auditing turned on before the change happens. Below I show what you can get right away (the time, the owner and Office’s own … Read more

How to Enable PowerShell Logging (Script Block and Module)

Enable PowerShell logging

To enable PowerShell logging, turn on Script Block Logging, which records the code of every script and command that runs. For Windows PowerShell 5.1, set this registry value as administrator: PowerShell 7 uses its own key and log. Below I check what’s already on, explain the three kinds of logging and show how to enable … Read more

How to Get BIOS Information Using PowerShell

Get BIOS information using PowerShell

To get BIOS information using PowerShell, query the Win32_BIOS class with Get-CimInstance. It shows the manufacturer, BIOS version, release date and serial number without a restart: It works without admin rights in PowerShell 7 and 5.1. Below I pick the useful properties, check UEFI and Secure Boot, query remote PCs and build an inventory report. … Read more

How to Find the Largest Folders Using PowerShell

Find the largest folders using PowerShell

To find the largest folders using PowerShell, add up the file sizes in each folder with Measure-Object and sort the results. Windows doesn’t store folder sizes, so you have to calculate them: Below I run it on a sample folder, show the top folders in friendly units, search every subfolder level and list the largest … Read more

PowerShell -match Operator: Regex Examples

PowerShell -match operator with examples

The PowerShell -match operator checks a string against a regular expression and returns True or False. When it matches, the matched text is saved in the automatic $Matches variable: Output: \d{4} means four digits, so the pattern finds the order number anywhere in the text. Below I cover case sensitivity, named groups, arrays, -notmatch, special … 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