PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Forum experts available to answer your PowerShell questions

Power Tips

Power Tips

Syndication

  • Get List of Type Accelerators

    Ever wondered what the difference between [Int] , [Int32] , and [System.Int32] is? They all are data types, and the first two are type accelerators, so they are really all the same. To list all the type accelerators PowerShell provides, use this undocumented...
  • Getting Help for Objects - Online

    In PowerShell 3.0, you finally can extend object types dynamically without having to write and import ps1xml-files. Here is an especially useful example: PS > $code = { $url = ' http://msdn.microsoft.com/en-US/library/{0}(v=vs.80).aspx ' -f...
  • Listing Currently Loaded Format Files

    The internal PowerShell formatting system (called ETS) relies on XML-based formatting data that comes from .ps1xml files. To see all files currently loaded by PowerShell and 3rd party extensions, use this line: $host . Runspace . InitialSessionState ...
  • Renaming Object Properties in Powershell

    Let's say you want to output just your top-level processes like this: PS > Get-Process | Where-Object { $_ . MainWindowTitle } | Select-Object Name , Product , ID , MainWindowTitle This works like a charm, but you'd like to rename the column...
  • Examine "Extended" Object Members

    PowerShell is based on .NET objects but often refines them by adding more. If you'd like to see just what PowerShell has added, use Get-Member with its parameter -View Extended. You'll be surprised how many useful properties are invented by PowerShell...
  • Writing Back WMI Property Changes

    Only a few properties in WMI objects are actually writeable although Get-Member insists they are all "Get/Set": PS > Get-WmiObject -Class Win32_OperatingSystem | Get-Member -MemberType Properties That's because WMI only provides "copies"...
  • Errors Travel in the Opposite Direction

    Here's a piece of code for you to wrap your head around: 1 . .10 | ForEach-Object { trap { Write-Host "Phew: $_" ; continue } $_ } | ForEach-Object { if ( $_ -gt 4 ) { Throw "Too big!" } else { $_ } } Ten numbers are fed into the...
  • Creating Dynamic Breakpoints

    The PowerShell 3.0 ISE editor has a simple debugger built-in that supports line breakpoints. Simply press F9 to set a breakpoint (which only works if the file has been saved). In addition, Set-PSBreakpoint can set dynamic breakpoints. This one would halt...
  • Creating "Mini-Modules"

    Did you know that every PowerShell function can be turned into a script module with just one line of code? To test drive this, open the ISE editor and create a function: function Get-BIOS { param ( $ComputerName , $Credential ) Get-WmiObject -Class Win32_BIOS...
  • Ripping All Links from a Website

    PowerShell 3.0 comes with a great new cmdlet: Invoke-WebRequest ! You can use it for a zillion things, but it can also simply retrieve the content of a website. It will even do basic parsing, so opening a window with all links on that website is a piece...
  • Splitting Hexadecimal Pairs

    If you'd have to process a long list of encoded information, let's say a list of hexadecimal values, how would you split the list into pairs of two? Here is a way: ' this gets splitted in pairs of two ' -split ' (?<=\G.{2})(?=....
  • Splitting Texts without Losing Anything

    Typically when you split a text using the -split operator or the Split() method, the split character is removed from the text: $profile -split ' \\ ' C : Users Tobias Documents WindowsPowerShell Microsoft.PowerShell_profile.ps1 If you want to...
  • Cutting Off Text at the End

    Cutting off a part of a text at its beginning is easy. This line eats the first 3 characters: PS > ' C:\folder\file.txt ' . SubString ( 3 ) folder \ file.txt Cutting off text at its end is not so easy because there is no method for it. You...
  • Use Comparison Operators for Logfile Parsing

    Comparison operators usually return either $true or $false , but when applied to an array, return the array elements that match the comparison. You can use this to easily parse text-based logfile information. This simple line returns all updates installed...
  • Use -f with N0

    Often, it is necessary to output numbers, but you may want to control the number of digits and would like to control the formatting. The -f operator can do this and has a trillion options but there's just one you need to remember: N0 (the "0"...
« First ... < Previous 26 27 28 29 30 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.