How to Find an HP Laptop Product Number Using PowerShell

Find an HP laptop product number with PowerShell

To find an HP laptop’s product number with PowerShell, read the SystemSKUNumber property of the Win32_ComputerSystem class. That’s the number printed on the sticker, the one HP support asks for: Output: I ran everything in this guide on an HP Pavilion laptop, in PowerShell 7.6 and Windows PowerShell 5.1. A product number and serial number … Read more

How to Find a File by Name in PowerShell

Find a file by name in PowerShell

To find a file by name in PowerShell, use Get-ChildItem with -Filter for the name and -Recurse to search every subfolder. This finds every file called notes.txt under a projects folder: Output: FullName gives you the complete path of each match. Name matching isn’t case-sensitive, so NOTES.TXT would find the same files. My test folder … Read more

How to Print a Variable in PowerShell (With Examples)

Print a variable in PowerShell

To print a variable in PowerShell, type its name on a line by itself. PowerShell sends the value to the output, which is the same as calling Write-Output. Write-Host prints it too: Output: All three lines look the same on screen, but they don’t behave the same inside functions and scripts. That difference is the … Read more

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

Convert a string to Base64 in PowerShell

To convert a string to Base64 in PowerShell, turn the text into bytes with [System.Text.Encoding]::UTF8.GetBytes(), then pass the bytes to [System.Convert]::ToBase64String(): Output: To decode Base64 back to text, reverse the two steps. FromBase64String() returns the bytes, and GetString() turns them into text: Output: I ran every example in PowerShell 7.6 and Windows PowerShell 5.1. The … Read more

PowerShell Naming Conventions: Functions, Parameters and Variables

PowerShell naming conventions

PowerShell naming conventions come down to one pattern for commands: an approved verb, a hyphen, and a singular noun, both in PascalCase. Parameters use PascalCase too. Here’s a function that follows all of it: Output: Anyone who knows Get-Service or Get-ChildItem can guess what Get-DiskReport does and how to call it. That’s the whole point … Read more

PowerShell Select-Object: Properties, -First, -Unique and More

PowerShell Select-Object examples

Select-Object is the PowerShell cmdlet that picks properties from objects, or picks a number of objects from a list. The examples below use this list of employees: To keep only some properties, list them after -Property: Output: I ran every example in real PowerShell 7.6 and Windows PowerShell 5.1 consoles. Where a parameter exists in … Read more

How to Check if a String Contains a Substring in PowerShell

Check if a string contains a substring in PowerShell

To check if a string contains a substring in PowerShell, use the -like operator with a * wildcard on each side. It returns True or False and ignores case: Output: Use -clike when case matters. For most checks, like looking for a server name or an error word in a log line, the case-insensitive -like … Read more

PowerShell Format-Table: Columns, Sorting and Long Text

PowerShell Format-Table examples

Format-Table is the PowerShell cmdlet that shows command output as a table. Pipe objects to it and list the properties you want as columns. All the examples below use this small list of servers: With no properties listed, Format-Table shows every property that fits: Output: I ran the examples in real PowerShell 7.6 and Windows … Read more

PowerShell Array Parameters: Pass Arrays to Functions

PowerShell array parameters

To make a PowerShell array parameter, give the parameter an array type such as [string[]]. Callers can then pass several values separated by commas, or just one value: Output: With [string[]], a single value still arrives as an array with one item, so the same foreach loop handles both calls. I tested every example in … Read more

PowerShell Test-Path: Check If a File or Folder Exists

PowerShell Test-Path examples

Test-Path is the PowerShell cmdlet that checks whether a file, folder or other path exists. It returns True or False, so it drops straight into an if statement: Output: It never throws an error for a missing path, which makes it the safest check before you read, copy or delete something. I ran each example … Read more