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
  • 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...
  • 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...
  • 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...
  • 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...
  • 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...
  • 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 parameters: function Get-Days { param ( [ DateTime ] $date ) [ Int ] ( New-TimeSpan $date ( Get-Date )). TotalDays...
  • 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 you want to make sure you get a truly unique ID. Use them to identify components or generate unique file...
  • 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: Get-Help about_Operators How do you know which other Help topics are available? Try this: Get-Help about_...
  • 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 To swap variables, list variables as comma-separated lists (actually, PowerShell treats your lists internally...
  • Automatic Aliases

    All Get-Cmdlets (cmdlets that start with "Get") have an automatic type accelerator. You can use those cmdlets without the verb. So Childitem is the same as Get-Childitem, and service lists services just like Get-Service.
  • 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. The following function tells you exactly that: function isAdmin { $identity = [ System.Security.Principal...
  • 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 ] $_ ) + "...
  • 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...
  • 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...
  • 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...
1 2 Next >
Copyright 2012 PowerShell.com. All rights reserved.