Using RegEx to Filter Files

Get-ChildItem supports basic wildcards, but it does not support the rich feature set of regular expressions. If you combine Get-ChildItem with Where-Object, you can easily add this functionality. This example lists all DLL files found in System32 folder that start...

Using "Using:" On Remote PowerShell Sessions

When you use PowerShell Remoting to execute scripts and commands on another machine, you may have run into an issue like this: $class = 'Win32_LogicalDisk' $ComputerName = 'storage1' Invoke-Command -ScriptBlock { Get-WmiObject -Class $class }...

Replacing Variable Names in ISE 3.0 Editor

If you want to replace variables in a PowerShell script, in PowerShell ISE 3.0 you can use the following function: function Replace-Variable { param( [Parameter(Mandatory=$true)] $OldName, [Parameter(Mandatory=$true)] $NewName ) $token=$null $text =...

Quick Reference Sheets for PowerShell

PowershellMagazine.com has prepared a set of very useful quick reference sheets for PowerShell 3.0, the ISE editor and more. Microsoft now offers these quick reference documents for download. They are free: http://www.microsoft.com/en-us/download/details.aspx?id=30002...

Reversing GUIDs

Active Directory and other services sometimes use a custom GUID format. To convert a GUID to that format, all blocks of the GUID need to be reversed. In addition, the second and third block need to be exchanged. PowerShell can easily do that conversion: $sampleGUID =...

Preventing Direct Function Calls

If you must make sure that a function within your script is inaccessible from outside calls, you can use this trick. Save this code as a script file, then run it: $scriptPath = $MyInvocation.MyCommand.Definition function Test { if ($MyInvocation.ScriptName -ne...

Use Select-String For Fast Textfile Parsing

Select-String is an extremely useful cmdlet for parsing log files. You can use it to dump all lines in a text file that contain a certain keyword. This dumps all lines from windowsupdate.log with the keyword "successfully installed": Select-String -Path...

Replace Escape

There are two ways for replacing text, and both have their advantages and pitfalls: Each string has a Replace() method which works very straightforward: Unfortunately, as you can see, the method is case-sensitive, and there is no way to change that. Alternatively, you...

Changing Scheduled Tasks with PowerShell

PowerShell can read and also change any part of a scheduled task. Just make sure you have appropriate permissions and run PowerShell elevated. Let's assume you wanted to automatically enable or disable a task trigger for a given task. Here's a sample that...

PowerShell 3.0 Help Available on Non-US-Systems

PowerShell 3.0 does not ship with help files. Instead, they need to be downloaded separately using Update-Help. Unfortunately, PowerShell 3.0 help isn't localized, yet, and only available in English. Non-English systems refuse to use the English help files,...

Latest Mac OS X Java Update to 1.6.0_51 Seems Broken

The update to 1.6.0_51 seems broken and causes ADS to no longer be functional. In diagnosing the problem, we have been able to add a small patch which will get Aqua Data Studio up and running, although we are not sure of the full extent of the Apple Java bug. The...

Examining Scheduled Tasks

There is a COM interface that you can use to select and dump any scheduled task definition. Just make sure you are running PowerShell with full Administrator privileges. This example dumps all tasks in Task Scheduler Library\Microsoft\Windows\DiskDiagnostic: $service...

Reversing Text Strings

Here's a simple trick to reverse a text string: $text = 'Hello World' $text = $Text.ToCharArray() [Array]::Reverse($text) -join $text The Reverse() method is very useful to reverse the order of any array. Since texts are just character arrays, it can be...

Running Portions of Code Elevated

Let's assume your script may or may not need to do a privileged operation, for example write a value to a HKEY_LOCAL_MACHINE branch, depending on some prerequisites. Instead of having to run the entire script with Administrator privileges, you can run only those...

Creating New Objects – Alternative

There are many ways in PowerShell to create new objects. One of the more advanced approaches uses a hash table to determine object properties and their values: $content = @{ User = $env:Username OS = (Get-WmiObject Win32_OperatingSystem).Caption BIOS = (Get-WmiObject...

Counting Number of Files – Fast!

If you need to know the number of files in a folder, there are different approaches with different advantages and disadvantages. The first one is using plain cmdlets and determines the number of files in the Windows folder: Get-ChildItem -Path $env:windir -Force |...