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
  • 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...
  • 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...
  • 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...
  • 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...
  • 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...
  • Creating Random Numbers

    Ever wanted to create an electronic dice (or needed random numbers for other purposes)? With PowerShell, simply instantiate a Random object and call its Next() method: $random = new-object random $random . Next (1,6) This gives you a new random number...
  • 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...
  • 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...
  • 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...
  • Turbo-Charging Arrays

    Simple arrays have no built-in mechanism to insert new elements or extract elements at given positions. For example, to extract the 5. element from an array, in PowerShell you'd have to work around it like this: $array = 1..10 $array = $array [0....
  • 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...
  • Calculating Space Consumption

    If you ever wanted to find out which folder in your profile consumes the most space (or want to check user profiles to make sure people do not overuse your resources), you can use this rather long pipeline command: Get-Childitem $home | Where-Object ...
  • Write-Protected Arrays

    Arrays are by default read/write so you cannot lock down arrays and make them read-only. To create a read-only array, you can "upgrade" it to an ArrayList, though. ArrayLists can then be converted into readonly arraylists: $array = 1..10 $array...
  • 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...
  • 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.