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

Power Tips

Power Tips

Syndication

  • 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"...
  • Sending Results to Excel

    Here's a little function called Out-ExcelReport . Just pipe anything to it, and it will open in Microsoft Excel - provided it is installed on your system: function Out-ExcelReport { param ( $Path = " $env:temp\$(Get-Random).csv " ) $Input...
  • Colors Become Even More Important in the New ISE Editor

    When you type in code into the new PowerShell 3.0 ISE editor, your code is not just "colorful". You may know that each color stands for a specific element, so variables are red and operators are gray. These colors can help you prevent errors...
  • Controlling Process Priority and Processor Affinity

    When you get yourself a process using Get-Process, what you get back is an object that has useful methods and writeable properties. This line will assign a high priority to your current PowerShell host and would run on all four CPU cores (provided your...
  • Executing Elevated PowerShell Code

    Sometimes, a script may want to execute some portion of its code elevated, for example to write to HKLM in the Registry or change protected settings. Instead of requiring the user to run the entire script elevated, you can execute portions of it in a...
« First ... < Previous 26 27 28 29 30 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.