Power Tips

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Powershell Plus


13 Free SQL Tools from Idera


Free Ebook: Mastering Powershell

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
  • Mapping Network Drives (Part 2)

    Beginning with PowerShell 3.0, you can use New-PSDrive to map network drives. They will be visible in File Explorer as well. Here is some sample code: #requires -Version 3 New-PSDrive -Name N -PSProvider FileSystem -Root ' \\dc-01\somefolder '...
  • Mapping Network Drives (Part 1)

    PowerShell supports console commands, so if you need to map a network drive, often the most reliable way is to use good old net.exe like this: #requires -Version 1 net.exe use M : ' \\dc-01\somefolder ' / PERSISTENT : YES Test-Path -Path M:\ explorer...
  • Executing with Timeout

    Start-Process can start processes but does not support a timeout. If you wanted to kill a runaway process after a given timeout, you could use an approach like this: #requires -Version 2 $maximumRuntimeSeconds = 3 $process = Start-Process -FilePath powershell...
  • Executing Selected Code as Admin

    If you need to run selected parts of your script with Administrator privileges, you could temporarily launch a second PowerShell with Administrator privileges, then run the privileged command in the temporary shell. Here is a sample that stops the Windows...
  • Finding Drive Letters

    Here is a simple function to find out the reserved drive letters: #requires -Version 3 function Get-DriveLetter { ( Get-WmiObject -Class Win32_LogicalDisk ) . DeviceID } To list all drive letters in use, try this: PS> Get-DriveLetter C: D: Y: Z: PS>...
  • Quickly Setting Multiple Environment Variables

    To quickly (and permanently) set a bunch of environment variables, here is a nice approach: $hashtable = @ { Name = ' Weltner ' ID = 12 Ort = ' Hannover ' Type = ' Notebook ' ABC = 123 } $hashtable . Keys | ForEach-Object { $Name...
  • Quickly Finding Scripts

    To quickly locate a PowerShell script anywhere in your MyDocuments folder, take a look at this Find-Script function: #requires -Version 3 function Find-Script { param ( [ Parameter ( Mandatory = $true )] $SearchPhrase , $Path = [ Environment ] :: GetFolderPath...
  • Hiding Variable Content

    When you override the ToString() method of an object you control how this object is displayed. The object content stays untouched, though: $a = 123 $a = $a | Add-Member -MemberType ScriptMethod -Name toString -Value { ' secret ' } -Force -PassThru...
  • Adding Additional Information to Objects

    When you retrieve results, you may want to add additional properties to the results so later you know where they came from. Attaching additional information to complex objects works a little different than attaching them to primitive data (as described...
  • Appending Extra Information to Primitive Data Types

    Maybe you'd like to tag a variable and provide some extra information. In PowerShell, use Add-Member and attach NoteProperties or ScriptProperties to it. A NoteProperty contains some static information, whereas a ScriptProperty runs code when you...
  • Simple Replacement for INI Files

    If you'd like to keep settings outside of your script and store them in a separate config file, then you can use all kinds of data formats for it. INI files lack native support so you would have to parse them manually. JSON and XML have parsers but...
  • Remove Array Elements

    Did you ever need to compare two arrays? Compare-Object might help. Check this out: $array1 = 1 . .100 $array2 = 2 , 4 , 80 , 98 Compare-Object -ReferenceObject $array1 -DifferenceObject $array2 | Select-Object -ExpandProperty InputObject The result is...
  • Quickly Getting IP Addresses

    You want to quickly get a list of IP addresses for your own computer or a network machine? Here is how: #requires -Version 3 $ComputerName = '' [ System.Net.Dns ] :: GetHostAddresses ( $ComputerName ) . IPAddressToString To only get IPv4, try...
  • Shortening Text

    Let's assume you want to chop off some text at the end of a string. This is the traditional approach using string operations: $text = "Some text" $fromRight = 3 $text . Substring ( 0 , $text . Length - $fromRight ) A much more powerful way...
  • Avoid Using Redirection

    While you can still use the old redirection operator to write command output to a file, you should rather use PowerShell cmdlets instead. Here is why: #requires -Version 2 $OutPath = " $env:temp\report.txt " Get-EventLog -LogName System -EntryType...
1 2 3 4 5 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.