March 2012 - 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
  • Shrinking Paths

    Many file-related .NET Framework methods fail when the overall path length exceeds a certain length. Use low-level methods to convert lengthy paths to the old 8.3 notation which is a lot shorter in many cases: function Get - ShortPath ( $Path ) { $code...
  • Output to Console AND Variable

    To assign results to a variable and at the same time view these results in your console, place the assignment operation into parenthesis: ( $result = Get-Process ) As you will see, the processes will still appear in your console. At the same time, they...
  • Converting to Signed Using Casting

    In a previous tip, you learned how to use the Bitconverter type to convert hexadecimals to signed integers. Here is another way that uses type conversion: PS > 0xffff 65535 PS > 0xfffe 65534 PS > [ int16 ]( "0x{0:x4}" -f ([ UInt32 ...
  • Converting to Signed

    If you convert a hex number to a decimal, the result may not be what you want: PS > 0xFFFF 65535 PowerShell converts it to an unsigned number (unless its value is too large for an unsigned integer). If you need the signed number, you would have to...
  • Getting Timezones

    Here's a low level call that returns all time zones: PS > [ System.TimeZoneInfo ] :: GetSystemTimeZones () Id : Dateline Standard Time DisplayName : ( UTC - 12 : 00 ) International Date Line West StandardName : Dateline Standard Time DaylightName...
  • Mapping Printers

    To map a network printer to a user profile, here's a powerful low level command: rundll32 printui.dll , PrintUIEntry /in /n "\\pntsrv1\HP552" This will map the printer share and also install drivers if required. A dialog window outputs progress...
  • Fun with Date and Time

    Get-Date can extract valuable information from dates. All you need to know is the placeholder for the date part you are after. Then, repeat that placeholder to see different representations. For example, an upper-case "M" represents the month...
  • Counting Work Days

    Ever wanted to know how many days you need to work this month? Here's a clever function that lists all the weekdays you want that are in a month. By default, Get-WeekDay returns all Mondays through Fridays in the current month. You can specify different...
  • Converting to Hex

    Here's a simple way to convert a decimal to a hex representation, for example, if you want to display an error number in standard hexadecimal format: PS > ( - 2147217407 ) . ToString ( "X" ) 80041001 Use a lower-case "x" if...
  • Sending Variables through Environment Variables

    Let's assume you want to launch a new PowerShell session from an existing one. To carry over information from the initial session, you can use environment variables: $env:payload = ' Some Information ' Start-Process powershell -ArgumentList...
  • Showing MsgBox

    Ever wanted to display a dialog box from PowerShell rather than spitting out console text? Then try this function: function Show-MsgBox { param ( [ Parameter ( Mandatory = $true )] [ String ] $Text , [ String ] $Title = ' Message ' , [ String...
  • ASCII Table

    Here’s a simple way of creating an ASCII table through type casting: 32 . .255 | ForEach-Object { ' Code {0} equals character {1} ' -f $_ , [ Char ] $_ } Likewise, to get a list of letters, try this: PS > [ Char []]( 65 . .90 ) A B (...
  • Pipeline Used Or Not?

    Sometimes you may want to know if your function received parameters over the pipeline or direct. Here is a way to find out: function test { [ CmdletBinding ( DefaultParameterSetName = ' NonPipeline ' )] param ( [ Parameter ( ValueFromPipeline...
  • Using Background Jobs to Speed Up Things

    PowerShell is single-threaded and can only do one thing at a time, but by using background jobs, you can spawn multiple PowerShell instances and work simultaneously. Then, you can synchronize them to continue when they all are done: # starting different...
  • Validate IP Addresses

    You can use regular expressions and the –match operator to validate user input. Here’s a loop that keeps asking until the user enters a valid IP address: $pattern = ' ^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4]...
1 2 Next >
Copyright 2012 PowerShell.com. All rights reserved.