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
  • Error handling for native commands

    When you need to handle errors created by native commands, you can use a wrapper function like Call. It will automatically discover when a native command writes to the error stream and return a warning: function Call { $command = $Args -join " "...
  • Calculate time zones

    If you need to find out the time in another time zone, you can convert your local time to Universal Time and then add the number of offset hours to the time zone you want to display: (( get-date ) . toUniversalTime ()) . AddHours ( 8 ) ReTweet this Tip...
  • Checking all event logs

    What if you would like to get a quick overview of all error events in any event log. Get-EventLog can only query one event log at a time. So, you can use -list to get the names of all event logs and then loop through them. The next line will get you the...
  • Creating IP segment lists

    If you need a list of consecutive IP addresses, you can check out this function. You can see that it takes a start and an end address and then returns all IP addresses in between: function New-IPSegment ( $start , $end ) { $ip1 = ([ System.Net.IPAddress...
  • Getting significant bytes

    If you need to split a decimal into bytes, you can use a function called ConvertTo-HighLow, which uses a clever combination of type casts to get you the high and low bytes: function ConvertTo-HighLow { param ( $number ) $result = [ System.Version ][ String...
  • Test Internet connection

    Try this line if you would like to know whether a machine is currently connected to the Internet: [ Activator ] :: CreateInstance ([ Type ] :: GetTypeFromCLSID ([ Guid ] ' {DCB00C01-570F-4A9B-8D69-199FDBA5723B} ' )) . IsConnectedToInternet It...
  • Making sure PowerShell scripts run in 32-bit

    If you are using code that can only run in a 32-bit environment (i.e. using old database drivers or COM objects), here is a solution that will re-launch the script in a 32-bit PowerShell when it is launched in a 64-bit environment: if ( $env:Processor_Architecture...
  • Running PowerShell on 64-bit systems

    On 64-bit systems, PowerShell will by default run in a 64-bit process. This can cause problems with snap-ins, some COM-objects (like ScriptControl) and database drivers that are designed to run in 32-bit processes. In this case, you can run them in the...
  • Splitting hex dumps

    Imagine you have a text string with a hex dump so that each hex number consists of two characters. How would you split this into individual hex numbers? Here is how: ' 00AB1CFF ' -split ' (?<=\G[0-9a-f]{2})(?=[0-9a-f]{2}) ' ReTweet...
  • Finding new processes

    Get-Process will return a list of all processes. If you just want to see those started within the last 10 minutes, you can check StartTime. Both lines will return processes that have been started within the past 10 minutes: PS > Get-Process | Where...
  • Load registry user hive

    If you need to manipulate registry data from another user, you may be out of luck because HKEY_CURRENT_USER always points to your own user data. However, you can manually load a user hive like this: reg.exe /load HKU\Testuser c:\users\tom\ntuser.dat PS...
  • Dump enumerations

    You can create a simple helper function called Get-Enum to list all the values in an enumeration: function Get - Enum ( $name ){ [ Enum ] :: GetValues ( $name ) | Select-Object @ { n = "Name" ;e = { $_ }} ,@ { n = "Value" ;e = { $_...
  • Compare services on two computers

    Ever wondered why one computer runs well and another does not? Here is an example of how you can compare service configuration on two machines as long as you have privileges in place to remotely access the other machine: PS > $own = Get-Service PS...
  • Use Foreach-Object instead of Select-Object -expandProperty

    As you may know, Select-Object can return the content of an object property when you use the parameter -expandProperty. This will get you a list of all software manufacturers of all running processes: Get-Process | Select-Object -expandProperty Company...
  • Appending text files without new line

    You can use Out-File with -Append to append lines to a text file, but there is no way to add text information without a line break. To do that, you can use .NET Framework directly. Here is an example: PS > "New Line1" | Out-File $env:temp...
« First ... < Previous 58 59 60 61 62 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.