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

Power Tips

Power Tips

Syndication

  • Using Nested Hash Tables

    Nested hash tables can be a great alternative to multidimensional arrays. They can be used to store data sets in an easy-to-manage way. Have a look: $person = @ {} $person . Name = ' Weltner ' $person . Id = 12 $person . Address = @ {} $person...
  • Speeding Up Arrays

    When you assign new items to an array often, you may experience a performance problem. Here is a sample that illustrates how you should not do it: Measure-Command { $ar = @ () for ( $x = 0 ; $x -lt 10000 ; $x ++ ) { $ar += $x } } In a loop, an array receives...
  • Using Event Logs Instead of Log Files

    Often, people use file-based logging. There is nothing wrong about that, but using the built-in event log system provided by Windows may be much easier. If you have admin privileges, you can create new event logs any time: New-EventLog -LogName myLog...
  • Reading Registry Values the Easy Way

    With PowerShell, it can be a piece of cake to read out Registry values. Here is your simple code template: $RegPath = ' HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion ' $key = Get-ItemProperty -Path "Registry::$RegPath"...
  • Handling Cmdlet Errors without Interruption

    When you want to use error handlers with errors that occur inside a cmdlet, then you can only catch such exceptions when you set the -ErrorAction of that cmdlet to "Stop". Else, the cmdlet handles the error internally. That's bad because...
  • Fun with Path Names

    You can use the -split operator to easily split a path in its components. The result is always an array. Simply use comparison operators to exclude any part component you do not like, or rename some, then use -join to put the path back together. This...
  • Skipping Profile on Keystroke

    Maybe you'd like to be able to occasionally skip certain parts of your profile script. For example, in the ISE editor, simply add this construction to your profile script (path to your profile script is found in $profile, it may not yet exist): if...
  • Using Profile Scripts

    You probably know that PowerShell supports profile scripts. Simply make sure the file found in $profile exists. It's a plain script that gets executed each time the PowerShell host launches. So it's easy to configure your PowerShell environment...
  • Be Aware of Side Effects

    There are plenty of low level system functions that PowerShell can use. This one, for example, creates a temporary file name: [ System.IO.Path ] :: GetTempFileName () However, it does not only do that. It also actually creates the file. So if you use...
  • Bulk File Renaming

    Let's assume you have a bunch of scripts (or pictures or log files or whatever) in a folder, and you'd like to rename all files. The new file name should, for example, have a prefix, then an incrementing number. Here's how you could do that...
  • Getting DateTaken Info from Pictures

    If you'd like to reorganize your picture archive, then here is a piece of code that reads the "DateTaken" information from picture files. The example uses a system function to find out the MyPictures path, then recursively searches for all...
  • Reading Installed Software Remotely

    Most software registers itself in the Registry. Here is a piece of code that reads all installed software from the 32-bit and 64-bit hive and works locally and remotely as well. It can serve as a good example on how to remotely read Registry keys, too...
  • gpupdate on Remote Machines

    To run gpupdate.exe remotely, you could use a script like this: function Start-GPUpdate { param ( [ String []] $ComputerName ) $code = { $rv = 1 | Select-Object -Property ComputerName , ExitCode $null = gpupdate.exe /force $rv . Exitcode = $LASTEXITCODE...
  • Getting Database Connection String

    Have you ever been puzzled just what the connection string would look like for a given database? When you create a new data source in Control Panel, a wizard guides you through the creation process. Here is a way to utilize this wizard and get back the...
  • Use Splatting to Encapsulate WMI Calls

    Splatting is a great way of forwarding parameters to another cmdlet. Here is an example that can be used to encapsulate WMI calls and make them available under different names: function Get-BIOSInfo { param ( $ComputerName , $Credential , $SomethingElse...
« First ... < Previous 3 4 5 6 7 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.