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
  • Duplicate Output

    Sometimes, you may want to store command results in a variable and at the same time, output it to the console. Once you assign results to a variable, however, the console will no longer show the results: PS > $result = Get-Process You should place...
  • Multiple Text Replace (Fast)

    In a previous tip, we showed you how to replace multiple different characters in a text using the Switch statement. While this works well, it is not very fast. The following approach is about 150-times faster: PS > $text = ' Österreich überholt...
    Filed under: ,
  • Speed Up Loops

    First, compare these two code samples: $array = 1 . .10000 Measure-Command { for ( $x = 0 ; $x -lt $array . Count ; $x ++ ) { $array [ $x ] } } Measure-Command { $length = $array . Count for ( $x = 0 ; $x -lt $length ; $x ++ ) { $array [ $x ] } } The...
    Filed under: ,
  • Set Clipboard

    If you are using Windows Vista or better, you can pipe text to clip.exe to copy it to your clipboard: Dir $env:windir | clip Here is another approach that you can use if your PowerShell host uses the STA mode: function Set-Clipboard { param ( $text )...
    Filed under: ,
  • Get-Clipboard

    If your PowerShell host uses the STA mode, you can easily read clipboard content like this: function Get-Clipboard { if ( $Host . Runspace . ApartmentState -eq ' STA ' ) { Add-Type -Assembly PresentationCore [ Windows.Clipboard ] :: GetText (...
    Filed under: ,
  • Test for STA mode

    By default, the PowerShell console does not use the STA mode whereas the ISE editor does. STA is needed to run Windows Presentation Foundation scripts and to use WPF-based dialog windows, such as Open. Here is how you can check to see whether your script...
  • Multiple Text Replace

    Imagine that you need to replace a number of different characters in a text. For example, you need to remove special characters or escape something. The switch statement can do that. You will just need to temporarily convert the text into a character...
  • Switch Accepts Arrays

    Did you know that the Switch statement can accept arrays? Use this sample to translate numbers into words: PS > switch ( 1 , 5 , 2 , 4 , 3 , 1 ) { 1 { ' one ' } 2 { ' two ' } 3 { ' three ' } 4 { ' four ' } 5 { '...
    Filed under:
  • Check For Numeric Characters

    Try this if you need to check a single character and find out whether or not it is numeric: PS > [ char ] :: IsNumber ( ' 1 ' ) True PS > [ char ] :: IsNumber ( ' A ' ) False The type Char has a bunch of other useful methods. Here...
  • Check Array Content With Wildcards

    You may know the -contains operator. Try using it to check whether an array contains a specific element: PS > $names = dir $env:windir | Select-Object -ExpandProperty Name PS > $names -contains ' explorer.exe ' True However, -contains does...
    Filed under:
  • Adding New PowerShell Drives

    You can add a bunch of interesting new drives to PowerShell with just a single line of code: [ System.Enum ] :: GetNames ([ System.Environment + SpecialFolder ]) | ForEach-Object { New-PSDrive -Name ( $_ ) -Root ([ System.Environment ] :: GetFolderPath...
  • Re-Assigning Types to Variables

    You can no longer assign other types when you strongly type a variable: [ Int ] $a = 1 $a = ' does not work ' However, you can always re-assign a new type to a variable: [ String ] $a = ' works ' ReTweet this Tip!
  • Generate Random Passwords

    In a previous tip, we showed you how to create random passwords. Thanks to your feedback, here is an even shorter version: -join ([ Char []] ' abcdefgABCDEFG0123456&%$ ' | Get-Random -count 20 ) ReTweet this Tip!
  • Tile Windows

    Do you remember the ancient feature where you could tile or cascade all open windows? These features are still available. However, before you try out the code, you should be aware that these features will re-size your windows and may change their default...
  • Case-Sensitive Hash Tables

    PowerShell hash tables are, by default, not case sensitive: PS > $hash = @ {} PS > $hash . Key = 1 PS > $hash . keY = 2 PS > $hash . KEY 2 If you need case-sensitive keys, you can create the hash table this way: PS > $hash = New-Object...
« First ... < Previous 57 58 59 60 61 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.