January 2009 - Power Tips

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

Welcome to the archive of tips delivered through Tobias' Tip of the Day RSS Feed and Your Power Tip of the Day email. Subscribe in the sidebar to get the latest tips!

Sort by: Most Recent | Most Viewed | Most Commented
  • 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 Select-Object to first limit the object properties to only those you want to see in your report, otherwise...
  • 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 create an array with drive letters is this: $letters = 65..89 | ForEach-Object { ([ char ] $_ ) + "...
  • Ping and Range Ping

    In PowerShell, you can access .NET methods directly so it is easy to add a ping functionality: $object = New-Object system.Net.NetworkInformation.Ping $object . Send ( '127.0.0.1' ) You can wrap it as function to get even more out of this: function...
  • 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 with the same LastWriteTime and Length are indeed equal. This line searches for duplicate PowerShell scripts...
  • 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} has {1:0.0} MB space available' -f $_ . Caption , ( $_ . FreeSpace / 1MB) } Here is how it works: Get...
  • 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 binary in one line: $number = 12756576 [ Convert ]:: ToString ( $number , 2) The toString() method takes...
  • 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 wrap the results in an array, use @(). You can then safely call the Count property to find out how many...
  • Grouping Folder Items by Extension (and more)

    Group-Object is a very powerful cmdlet as it takes one or more object properties and uses them to group the items. To get a quick overview of just how many different files are stored in a folder, use the file object's extension property: Dir $env...
  • 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 just wanted to find out how much space is left on drive C:? The next line gives you all drives: Get-WMIObject...
  • 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 disk drives. All size properties return bytes so you may want to show megabytes or gigabytes: Get-WMIObject...
  • Listing Folders Only (and Finding Special Folders)

    Get-Childitem returns both files and folders. If you just want to see folders, use a filter-based on folders that have a property called PSIsContainer, that is true. The following line lists all folders and subfolders in your Windows folder: dir $env...
  • 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 "C:" | Format-List * $drive = New-Object System.io.DriveInfo "C:" $drive . DriveFormat...
  • 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. If no name is specified, the function outputs a warning message and exits the function using return: function...
  • 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 this format: function isEmailAddress( $object ) { ( $object -as [ System.Net.Mail.MailAddress ]). Address...
  • 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: function isIPAddress( $object ) { ( $object -as [ System.Net.IPAddress ]). IPAddressToString -eq $object -and...
1 2 Next >
Copyright 2012 PowerShell.com. All rights reserved.