Powershell

Calling ChkDsk via WMI

Some WMI classes contain methods that you can call to invoke some action. For example, the next line initiates a disk check on drive D:...

Getting Help for WMI Classes

In a previous tip, you discovered how to search for WMI classes like Win32_VideoController. Now, what exactly is the purpose of this class? You can...

Finding Interesting WMI Classes

WMI is a huge repository. If you want to get to useful information, you will need to specify the name of a valid WMI class. Fortunately, it is easy...

Rename Drive Label

WMI can also read any drive label (the name that appears next to a drive inside Explorer), and you can change the drive label, too—provided...

Remove Empty Entries

One little known fact is that Where-Object is a cool and simple way of removing empty objects. Let's say you want to list all network adapters...

Finding Your Current Domain

Try this quick and simple way to find out the domain name that you are currently connected: [ADSI]"" The domain name is returned if you...

Retrieving Clear Text Password

Get-Credential is a great way of prompting for credentials, but the Password you enter into the dialog will be encrypted. Sometimes, you may need a...

Use PowerShell Cmdlets!

Whenever possible, try to avoid raw .NET access if you would like to create more readable code. For example, the following line returns the current...

Finding Available .NET Frameworks

Try this if you would like to get a quick overview of all installed .NET framework versions: dir $env:windir\Microsoft.NET\Framework\v* -name It...

Secret Timespan Shortcuts

Usually, to create time spans, you will use New-Timespan. However, you can also use a more developer-centric approach by converting a number to a...

Finding Secret Date-Time Methods

However, to use developer-centric date-time methods, you will need to know them first. Here is how: Get-Date | Get-Member -memberType *Method This...

Adding and Subtracting Time

There are actually two different ways of manipulating dates and times. Here is the high-level approach to subtract 30 days from today: (Get-Date) -...

Creating Time Spans

A time span represents a time interval, and you can use time spans to subtract or add time to a date. For example, to get the date seven days ago:...

Make Function Parameters Mandatory

Mandatory parameters are special because if you do not submit it, PowerShell will automatically ask for it. You can use mandatory parameters in your...

Add Help to Your Functions

You should simply copy and paste the following block comment right above your functions and magically Get-Help works with your functions, too!...

Opening Current Folder in Explorer

Ever wanted to open the current PowerShell folder in an Explorer view? Try this: ii . This will work as long as the current folder is a valid file...

Get Running Process Owners

If you need to filter running processes by owner, for example to terminate the ones owned by some user, you should use WMI and the GetOwner()...

Check for a Battery

If your script needs to know whether your computer has a battery, you can ask WMI. Here is a small function: function Has-Battery {@(Get-WmiObject...

Store Pictures in User Accounts

Have you ever wondered how PowerShell would store a jpeg picture into your Active Directory user account? With the help of some low-level .NET...

Accessing Function Parameters by Type

Adding parameters to your functions is fairly easy. You can add a param() block and specify the parameters. But what if you wanted to assign values...

Creating Your own Types

Did you know that you can compile any .NET source code on the fly and use this to create your own types? Here is an example illustrating how to...

Calculating Server Uptime

Have you ever wanted to find out the effective uptime of your PC (or some servers)? The information can be found inside the event log. Here is an...

Getting Installed Updates

PowerShell is great for parsing log files. Here is a function that extracts all installed Windows updates from an internal log file and returns the...

E-mail-Address-Extractor via RegEx

Regular expressions are extremely powerful - and complex. Fortunately, there are plenty of sources for good regular expressions that describe all...

Accessing WMI Instances in One Line

While you can use Get-WMIObject to query for WMI objects and then select the ones you are really after, you can also cast a WMI object path to a WMI...

Grouping with Script Blocks

Group-Object creates groups based on object properties. For example, you could group processes by company or folder listings by extension: Dir...

Replace Text in Files

Often, some text will need to be replaced in a text file. That's easy with Get-Content and Set-Content - or not? Get-Content c:somefile.txt |...

1 88 89 90 91 92 104