powertips

Persisting Comparison Snapshots

Whenever you'd like to compare long-time results or compare data on different machines, you want to use persisted result sets. Persisted result...

Finding Folder Changes

Compare-Object can help you monitor folder content and find changes. To monitor, first create an initial snapshot. At a later time, you can then...

Comparing Results

PowerShell makes it easy to compare results and find only things that changed. For example, you may want to list only processes that started after a...

Setting Properties on AD Users

If you'd like to find Users in your Active Directory and bulk-change certain properties, you may not need 3rd party extensions. Here is an...

Calculating Space Consumption

If you ever wanted to find out which folder in your profile consumes the most space (or want to check user profiles to make sure people do not...

Write-Protected Arrays

Arrays are by default read/write so you cannot lock down arrays and make them read-only. To create a read-only array, you can "upgrade" it...

Turbo-Charging Arrays

Simple arrays have no built-in mechanism to insert new elements or extract elements at given positions. For example, to extract the 5. element from...

Creating Numeric Ranges

There is a clever trick in PowerShell to create numeric ranges that you probably know: 1..10 But did you know you can use variables with this trick?...

Creating Random Numbers

Ever wanted to create an electronic dice (or needed random numbers for other purposes)? With PowerShell, simply instantiate a Random object and call...

Creating Lists of Letters

The easiest way to create an array of letters is to convert an array of numbers into an array of characters like this: $letters = [char[]](97..122)

Sorting Arrays

Let's assume you have an array of items which you would like to sort. Here is the PowerShell way: $array = 1,5,32,5,7$array | Sort-Object$array...

Analyzing URLs

URLs contain a lot of information which can be automatically parsed by PowerShell. Simply convert a URL to the System.URI type. Once you did this,...

Escaping Text Strings

HTML on web pages uses tags and other special characters to define the page. To make sure text is not misinterpreted as HTML tags, you may want to...

Checking Host Name Type

To check whether a string contains a valid host name, you can use the CheckHostName() method provided by the System.URI type. It will return...

Validating a URL

To make sure user input is a valid URL, you can use the System.URI type. Try to convert the raw string into this type. If it works, the string is a...

Reversing Array Order

To reverse the order of elements in an array, the most efficient way is to use the [Array] type and its static method Reverse(): # Create an array...

Limiting Variables to a set of values

By adding a ValidateSetAttribute to a variable, you can force it to accept only values that match a given set. Once you add this attribute in the...

Validating IP-Addresses

To check for a valid IP-address, use the .NET Framework type System.Net.IPAddress and test whether the data can be converted into this format:...

Validating Email-Addresses

To check for valid email addresses, you can use the .NET Framework type System.Net.Mail.MailAddress and test whether the data can be converted into...

Exiting a Function

To exit a function immediately, use the return statement. The next function expects a name (including wildcards) and lists all matching processes....

Quick Drive Info

Want to quickly get a number of interesting details for any drive? Use the .NET System.IO.DriveInfo class like this: New-Object System.io.DriveInfo...

Ping and Range Ping

In PowerShell, you can access .NET methods directly so it is easy to add a ping functionality: $object = New-Object...

Validating User Input

When writing a function that accepts parameters, you can strongly-type parameters so that an exception occurs when the user submits the wrong...

Generate a New GUID

GUIDs are "Globally Unique Identifiers," which are so random that you can safely assume they are unique worldwide. GUIDs are used whenever...

Discover about-Topics

PowerShell comes with a lot of documentation. It is just sometimes hard to find. For example, to get a list of all available operators, do this:...

Assigning Multiple Variables

In PowerShell, you can initialize multiple variables in just one line. The following line sets all variables to the value 1: $a = $b = $c = $d = 1...

Automatic Aliases

All Get-Cmdlets (cmdlets that start with "Get") have an automatic type accelerator. You can use those cmdlets without the verb. So...

Exploring Privileges

On Vista with UAC enabled, you are not Admin by default. It might be interesting to find out if PowerShell currently has Admin privileges enabled....

Enumerating Drive Letters

Sometimes, you may want to find the next available drive letter for a network drive or enumerate drive letters for other purposes. An easy way to...