database-tools

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...

Finding (and Deleting) Duplicate Files

There are numerous ways of finding duplicate files. One approach uses Group-Object and groups your files by LastWriteTime and Length, assuming files...

Counting Items in a Folder

Get-Childitem returns all files in a folder. PowerShell returns an array if there are at least two items in a folder. To force PowerShell to always...

Converting Numbers

The .NET convert class is a great help when you need to convert numbers between different systems. Here's how you can convert a decimal into a...

Outputting HTML Reports

PowerShell can export results as HTML. Simply pipe the results to ConvertTo-HTML and save the result in a file. When you do that, it is wise to use...

Add Custom Properties

While objects contain a wealth of information, this information sometimes isn't in the right format. Let's take WMI objects representing...

Accessing Individual WMI Instances

You always get back all instances of a given WMI class when using Get-WMIObject. However, what if you just wanted to get a specific instance? Or you...

Free Space on Disks

You can use WMI to determine how much free space is available on any given disk: Get-WMIObject Win32_LogicalDisk | Foreach-Object { 'Disk {0}...

Converting User Input to Date

PowerShell uses the US/English date format when converting user input to DateTime, which can cause unexpected results if using a different culture....

Casting a Type Without Exception

Read-Host is a useful cmdlet to use to ask for user input. However, it returns user input always as generic string. Of course, you can always...

Order Matters

Here is a challenge for you. The following code is a simple currency converter. However, when you run it, you'll notice it doesn't convert...

Filtering Based On File Age

Every so often, you'll need to filter files by age. Maybe you'll only want to see files that are older than 20 days old and delete them or...

Accessing Date Methods

While Get-Date returns the current date and time, it really returns a DateTime object. You can use this object to find out more about the date or to...

Using Cultures

Since PowerShell is culture-independent, you can pick any culture you want and use the culture-specific formats. The following script instantiates...

Outputting Nicely Formatted Dates

Get-Date provides you with the current date and time. With the -format parameter, you can add style to it. For example, use -format with a lowercase...

Stopping and Disabling Services

You may find that Vista's new Instant Search can sometimes get out of hand and slow down your machine. Temporarily disabling and then stopping...