November 2008 - 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
  • Finding System Folders

    When you automate file system tasks, you may want to know where special folders such as MyPictures or Documents are located. The .NET class Environment provides a static method named GetFolderPath() which provides this information. To find the location...
  • Finding Out a Scripts Parent Folder

    If you need to find helper files that are stored in the same folder, you may want to know where a given script is stored. The automatic variable $MyInvocation can provide this information. The following commands tell you the script name and the parent...
  • Finding the Current User

    Should you try and use PowerShell as a log-on script, you may want to know who is actually running the script to access user specific folders or settings. The .NET Environment class can tell you who is running a script. [ Environment ]:: UserDomainName...
  • Outputting Calculated Properties

    Format-Table is a very convenient cmdlet to output data as table. You can pick the object properties you want to output like this: Dir | Format-Table Name, Length This would then give you a table with file names and file size in bytes. However, if you...
  • Finding Old Files

    Occasionally, you might want to find files that are older than a give number of days to delete or backup those. A simple filter can provide that functionality: filter FileAge( $days ) { if ( ( $_ . CreationTime -le ( Get-Date ). AddDays ( $days * - 1...
  • Working with Arrays

    Creating arrays in PowerShell is easy using the comma delimiter. The next line creates an array with five elements: $myArray = 'Hello' , 12, ( Get-Date ), $null , $true $myArray . Count To access array elements, use square brackets and the element...
  • Converting Results into Arrays

    Whenever you call a function or cmdlet, PowerShell uses a built-in mechanism to handle results: If no results are returned, PowerShell returns nothing If exactly one result is returned, the result is handed to the caller If more than one result is returned...
  • Finding Duplicate Files

    Hash Tables are a great way to find duplicates. Simply use the Hash Table as lookup to see if the file (or element) was already added to the Hash Table. The following script would find all files and folder with same name in the current folder, the windows...
  • Converting Hash Tables to Objects

    Hash Tables are convenient but are not true objects. This is bad because you are unable to output the hash content to formatting cmdlets or export cmdlets. With a short function, you can easily convert a Hash Table to an object, providing all the flexibility...
  • Sorting Hash Tables

    Hash Tables store key-value pairs, and you normally cannot sort its content. Let's define a Hash Table first to examine this: $hash = @{Name = 'Tobias' ; Age = 66; Status = 'Online' } When you output its content, you get two columns...
  • Using Hash Tables

    Hash Tables are a great way to organize data. A hash table stores key-value-pairs. To create a new hash table variable, try this: $person = @{} You can then add new key-value-pairs by simply using the dot notation: $person . Age = 23 $person . Name =...
  • Strongly Typed Variables

    Unless you override how PowerShell stores variable content, you may find that PowerShell does not automatically pick the best type. For example, when you assign a date to a variable, the date is stored as string: PS> $date = 'November 12, 2008'...
  • Permanent Changes to Environment Variables

    You can easily manage environment variables with the predefined env: drive. For example, to add a new environment variable, type this: $env:launched = $true However, these changes are visible only within the current PowerShell session because all changes...
  • Making Variables Constant

    If you need to write more robust scripts, it makes sense to write-protect certain variables. Whenever you want a variable to set its content as unchangeable, add a write-protection flag: $server = '10.10.10.10' Set-Variable server -option ReadOnly...
  • Add Descriptions to Variables

    Keeping track of a variable’s purpose can be accomplished by assigning a clear text description: $ip = '10.10.10.10' Set-Variable ip -description 'Server IP or Name' When you now list your variables, you can output the variable description...
1 2 Next >
Copyright 2012 PowerShell.com. All rights reserved.