Turning CSV-Files into "Databases"

Let's assume you have a CSV file with information that you need to frequently look up. For example, the CSV file may contain server names and certain configuration settings for them. To easily look up items in your CSV file, you can turn it into a hash table....

Testing Event Log Names and Sources

Write-EventLog lets you write custom entries to event logs, and New-EventLog can add new event logs and event sources. Which raises the question: how can you test in advance whether a given event log or event log source exists? Here's how: # check if event log...

Writing Text Information Fast

If you want to write plain text information to a file, don't use Out-File. Instead, use Set-Content. It is much faster: $tempfile1 = "$env:temp\tempfile1.txt" $tempfile2 = "$env:temp\tempfile2.txt" $tempfile3 =...

Quickly Replace Words in Text File

In a previous tip we explained how you can convert a string array into one big string. That's a prerequisite for quickly searching and replacing instances of words in a text file. This example takes windowsupdate.log and replaces all instances of "error"...

Converting String Array in String

When you use Get-Content to read the content of a text file, you always get back a string array. So each line of your text file is kept separately. If you want to convert a string array into one large string, use the operator -join: $file =...

Keeping MsgBox On-Top

When you open a MsgBox dialog from PowerShell, the dialog window may sometimes not be visible and instead appears behind the PowerShell or ISE window. To make sure a MsgBox dialog box appears in front of your PowerShell window, try this: Add-Type -AssemblyName...

Calculate Broadcast Address

If you know the IP address and subnet mask, you can take these and calculate the broadcast address. Here's a function does it for you. Simply submit IP address and subnet mask, and receive the broadcast address: function Get-BroadcastAddress { param (...

Convert IP address to decimal value (and back)

Sometimes you may want to convert an IP address to its decimal value, for example, because you want to use binary operators to set bits. Here are two simple filters that make this a snap: filter Convert-IP2Decimal { ([IPAddress][String]([IPAddress]$_)).Address }...

Running Portions of Code in 32-bit or 64-bit

To execute code in 32-bit from within a 64-bit environment (or vice versa), you can create appropriate aliases: In a 32-bit PowerShell, you create: Set-Alias Start-PowerShell64 "$env:windir\sysnative\WindowsPowerShell\v1.0\powershell.exe" And in a 64-bit...

Prompt for Credentials without a Dialog Box

Whenever a PowerShell script asks for credentials, PowerShell pops up a dialog box. You can view this by running this command: Get-Credential PowerShell is a console-based scripting language, and so it may be unwanted to open additional dialogs. That's why you can...

Finding User Group Memberships

If you run a large Active Directory, you should use specific Active Directory cmdlets or management functions. However, if you just want to know the groups a given user account belongs to, and if the user account can also be a non-domain local account, then WMI may...

Finding User Account with WMI

WMI represents all kinds of physical and logical entities on your machine. It also has classes that represent user accounts which include both local and domain accounts. This piece of code returns the user account of the currently logged on user: Get-WmiObject -Class...

Displaying IPv4 address as Binary

Sometimes it may be useful to display an IPv4 address bit by bit, for example, to compare it with a subnet mask. Here's another example of just how flexible PowerShell is because it only takes one line: $ipV4 = '192.168.12.33' -join...

Shortening Parameter Arguments in PowerShell 3.0

In PowerShell 3.0, when a parameter expects a fixed list of values, you do not need to write the complete argument. It is OK to write just as much as is needed to uniquely identify the argument: Write-Host 'This is a colorful message' -ForegroundColor r...

Resolving URLs

Sometimes you may stumble across URLs like this one: http://go.microsoft.com/fwlink/?LinkID=13517 As it turns out, these are just "pointers" to the real web address. In PowerShell 3.0, the new cmdlet Invoke-WebRequest can resolve these URLs and return the...

Working With TimeSpan Objects

TimeSpan objects represent a given amount of time. They are incredibly useful when you calculate with dates or times because they can represent the amount of time between two dates, or can add a day (or a minute) to a date to create relative dates. Here are some...

Converting Low-Level Error Numbers into Help Messages

Sometimes, native commands such as net.exe return cryptic error numbers instead of full error messages. Traditionally, you could use the following command to convert these error numbers into full messages: PS> net helpmsg 3534 The service did not report an error....