How to Kill a Process by Name in PowerShell

Kill a process by name in PowerShell

To kill a process by name in PowerShell, run Stop-Process -Name followed by the process name, without .exe. It stops every running process with that name: For the tests in this guide, I started a few hidden ping.exe processes, so nothing real was harmed. Here the command stops two of them: Output: I ran every … Read more

How to Convert Variables to Strings in PowerShell

Convert a variable to a string in PowerShell

To convert a variable to a string in PowerShell, cast it with [string], call its ToString() method, or put it inside double quotes. For a number, all three give the same text: Output: They don’t always agree, though. Dates, decimals, arrays and $null each behave a little differently depending on which one you use, and … Read more

PowerShell Append to Array: +=, Lists and Common Mistakes

Append to an array in PowerShell

To append to an array in PowerShell, use the += operator. It adds the new item to the end, so a list of two servers becomes three: Output: That’s all you need for small scripts. But += doesn’t really grow the array. It builds a brand-new one each time, which explains most of the surprises … Read more

How to Prompt for Input in PowerShell (Read-Host Examples)

Prompt for user input in PowerShell with Read-Host

To prompt for input in PowerShell, use Read-Host. It shows your message, waits until the user types something and presses Enter, and returns what they typed: Output (what I typed follows the colon): PowerShell adds the colon and space after your prompt text, so leave them out of the message. The value goes into $name, … Read more

How to Convert a Byte Array to a String in PowerShell

Convert a byte array to a string in PowerShell

To convert a byte array to a string in PowerShell, decode it with the matching text encoding, usually UTF-8. [System.Text.Encoding]::UTF8.GetString() takes the bytes and returns the text: Output: The reverse is GetBytes(), and those two methods are most of what you need. The rest of this guide covers picking the right encoding, creating byte arrays, … Read more

How to Get Folder Size in PowerShell (Including Subfolders)

Get folder size in PowerShell

To get a folder’s size in PowerShell, list every file under it with Get-ChildItem -Recurse and add up their Length values with Measure-Object. The result is in bytes, so divide by 1MB or 1GB: Output: A folder doesn’t store its own size, so PowerShell has to add up the files, just like the Properties window … 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 Create a Credential Object in PowerShell

Create a PSCredential object in PowerShell

To create a credential object in PowerShell, turn the password into a SecureString and pass it with the user name to [pscredential]::new(). Any cmdlet with a -Credential parameter accepts the result: Output: The password above is a throwaway demo value. In a real script, you’d ask for it with Get-Credential or load it from a … Read more

How to Format Dates in PowerShell (Get-Date -Format Examples)

Format dates in PowerShell with Get-Date

To format a date in PowerShell, pass a format string to Get-Date -Format. MM is the month, dd the day, yyyy the year, HH the hour on a 24-hour clock and mm the minutes: Output: Every example here uses the same fixed date, September 28, 2026 at 2:05:09 PM, so your output will match mine. … Read more

How to Check if an Array Contains a Value in PowerShell

Check if a PowerShell array contains a value

To check if an array contains a value in PowerShell, use the -contains operator. It returns True or False, and it ignores case: Output: That covers the everyday case. Below, I’ll handle the variations people actually search for: case-sensitive checks, partial matches, “does not contain”, several values at once, the index of an item, and … Read more