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
  • Adding a Hash property to file objects

    You can easily add new properties and functions to object types using the extended type system. In this example, you append file objects with a new script property which creates a file hash. File hashes are an excellent way of finding duplicate files...
  • Adding File Age to file objects

    PowerShell cmdlets return objects with rich information, and to see all the information, you can pipe the result to Format-List *: Dir $env:windir | Format-List * You can easily append more properties if the information returned is not sufficient. For...
  • Using Calculated Columns in New Objects

    Select-Object can add new properties to objects and fill them with calculated content. All you need for this is a hash table with two pieces of information: "Name" (which is the new property name) and "expression" (which is the code...
  • Use Hash Tables for Custom Columns

    Hash tables can generate "calculated" columns. All you need to do is store two pieces of information: a "label," which serves as new column header, and an "expression," which is the code used to calculate the column: $age...
  • Creating New Objects

    There is a powerful new way in PowerShell v.2 to create new objects from scratch: You should first get yourself an empty hash table object, then add all the information to the hash table and finally use New-Object with its new -Property parameter to turn...
  • Exporting and Importing Credentials

    You may have to use alternate credentials when you connect to different systems. You’ll find that a lot of cmdlets provide the -credential parameter, which accepts a credential object. Normally, you would create such an object by using Get-Credential...
  • Exit a Script Immediately

    Use the Exit statement if you run into a condition where a script should quit. The script breaks whenever you call exit from within your script. Add a number to Exit to set the error level (return code). The caller of your script can then check the automatic...
  • Get Remotely, Store Locally

    When you start using PowerShell remotely, you may run into situations like this one: Invoke-Command -ComputerName remotepc { Get-Process | Select-Object Name | Export-Csv c:\ result.csv } Here, PowerShell gets all processes from "remotepc,"...
  • Dynamically Create Script Blocks

    It is sometimes necessary to dynamically create script blocks. Here is an example how and why: function Get-RemoteLog ( $name ) { $code = "Get-EventLog -LogName $name -Newest 5 -EntryType Error " $sc = $executioncontext . InvokeCommand.NewScriptBlock...
  • Remove Registry Keys and Values

    Use Remove-Item to actually delete a registry key: function Remove-RegistryKey ( $key ) { Remove-Item $key -Force } Use Remove-ItemProperty to delete a registry value: function Remove-RegistryValue ( $key , $value ) { Remove-ItemProperty $key $value ...
  • Using Real Registry Keys

    In PowerShell, you access registry keys through virtual drives like HKCU: (for HKEY_CURRENT_USER) and HKLM: (representing HKEY_LOCAL_MACHINE). You do not need these drives, though. Use the traditional registry key name and prepend "Registry::"...
  • Writing to the Registry

    Adding or changing registry values is not always intuitive with PowerShell. Here is a function called Set-RegistryValue that will make your life easier: function Set-RegistryValue ( $key , $name , $value , $type = "String" ) { if (( Test-Path...
  • Reading Registry Values

    In PowerShell, you will need to use one of the Registry virtual drives to read from the Windows Registry as it is - not always intuitive. Here are two functions to make your life easier. Get-RegistryValues lists all values stored in a registry key: function...
  • Running Commands On Multiple Computers

    You can work remotely not just for single computers. You can also run your commands against multiple computers like this: $computer = Get-Content computers.txt $session = New-PSSession -ComputerName $computer Invoke-Command -Session $session { md hklm...
  • Configuring WSMan Remotely for multiple computers

    When working remotely in a peer-to-peer or cross-domain scenario, you will have to add all the computers you'd like to communicate with into the trusted hosts list. Unfortunately, when you try this, any new entry will overwrite the existing one, so...
« First ... < Previous 58 59 60 61 62 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.