February 2009 - Power Tips

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
  • Limiting Variables to a set of values

    By adding a ValidateSetAttribute to a variable, you can force it to accept only values that match a given set. Once you add this attribute in the next example, the variable $option can only accept the values yes, no or perhaps: $option = "yes"...
  • Setting Properties on AD Users

    If you'd like to find Users in your Active Directory and bulk-change certain properties, you may not need 3rd party extensions. Here is an example based solely on PS that finds all users with dialin permissions and resets the permissions: $searcher...
  • Cleaning Document Folders

    Often, in your document folders a lot of files exist, and most of the time they are not really organized. With the help of a little PowerShell script, you can easily clean up and organize your files based on file type. Warning : The following script will...
  • Limiting Variables to a certain Length

    With strong typing, you can limit a variable to only a certain data type such as String: [ String ] $a = 'Hello' By adding a ValidateLengthAttribute, you can further limit the variable to only text of certain length. The following code limits...
  • Comparing Results

    PowerShell makes it easy to compare results and find only things that changed. For example, you may want to list only processes that started after a given time. To do that, first create the initial snapshot of running processes. Then, at a later time...
  • Finding Folder Changes

    Compare-Object can help you monitor folder content and find changes. To monitor, first create an initial snapshot. At a later time, you can then compare the current folder content against that snapshot: $shot1 = Dir $home Set-Content $home \ testfile1...
  • Persisting Comparison Snapshots

    Whenever you'd like to compare long-time results or compare data on different machines, you want to use persisted result sets. Persisted result sets do not live in memory but are instead written to xml as file. This way, you can collect data from...
  • Advanced Compare-Object: Working with Results

    By using the -passThru parameter, you can tell Compare-Object to return the actual objects you compared. Let's say you are looking for new processes started after a given point in time. You could easily create a baseline snapshot: $base = Get-Process...
  • Reversing Array Order

    To reverse the order of elements in an array, the most efficient way is to use the [Array] type and its static method Reverse(): # Create an array containing text and output contents: $a = ipconfig $a # Reverse array contents and then output it again...
  • Validating a URL

    To make sure user input is a valid URL, you can use the System.URI type. Try to convert the raw string into this type. If it works, the string is a valid URI. You can then further examine the converted result to limit validation to only http/https URLs...
  • Checking Host Name Type

    To check whether a string contains a valid host name, you can use the CheckHostName() method provided by the System.URI type. It will return "Unknown" for any invalid host name. If you specify a valid host name, the method tells you whether...
  • Escaping Text Strings

    HTML on web pages uses tags and other special characters to define the page. To make sure text is not misinterpreted as HTML tags, you may want to escape text and automatically convert any ambiguous text character in an encoded format. Use EscapeDataString...
  • Analyzing URLs

    URLs contain a lot of information which can be automatically parsed by PowerShell. Simply convert a URL to the System.URI type. Once you did this, all information contained in the URL is accessible via individual properties: $result = [ System.uri ] 'http...
  • Sorting Arrays

    Let's assume you have an array of items which you would like to sort. Here is the PowerShell way: $array = 1,5,32,5,7 $array | Sort-Object $array = "Hello" , "World" , "Test" , "A" , "Z" $array | Sort...
  • Creating Lists of Letters

    The easiest way to create an array of letters is to convert an array of numbers into an array of characters like this: $letters = [ char []](97..122)
1 2 Next >
Copyright 2012 PowerShell.com. All rights reserved.