June 2010 - 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
  • Changing Error Background Color

    If you would like to make error messages more readable, you can change their background color from black to white: $host . PrivateData.ErrorBackgroundColor = 'White' Use this line to see the colors that you can assign: [ System.Enum ]:: GetNames...
  • Replace Text in Files

    Often, some text will need to be replaced in a text file. That's easy with Get-Content and Set-Content - or not? Get-Content c:\ somefile.txt | Foreach-Object { $_ -replace 'old' , 'new' } | Set-Content c:\ somefile.txt If you try...
  • E-mail-Address-Extractor via RegEx

    Regular expressions are extremely powerful - and complex. Fortunately, there are plenty of sources for good regular expressions that describe all kinds of patterns. This is how you turn a RegEx pattern into a regular expression engine: $regex = [ RegEx...
  • Finding Object Types

    When you pipe the result of a command to Get-Member, it examines the returned objects and shows the properties and methods. Use this line if you would like to focus on the different object types a command returns: Dir $env:windir | Get-Member | Group...
  • Filtering Day of Week

    You can also use this little helper function if you run scripts every day and want to make sure they don't do anything on weekends: function is-Weekend { ( Get-Date ). DayOfWeek -gt 5 } You can use it like this: If ( is-Weekend ) { 'no service...
  • Removing Empty Things

    How do you exclude objects based on empty properties? For example, WMI returns all kinds of "network adapters:" Get-WMIObject Win32_NetworkAdapterConfiguration To focus only on those that have an IPAddress assigned, you should exclude any object...
  • Getting Installed Updates

    PowerShell is great for parsing log files. Here is a function that extracts all installed Windows updates from an internal log file and returns the information as pure PowerShell objects. Have a look as this code uses a number of powerful parsing and...
  • Calculating Server Uptime

    Have you ever wanted to find out the effective uptime of your PC (or some servers)? The information can be found inside the event log. Here is an example on how to select and prepare event data and create a report. This even works remotely. function Get...
  • Creating Your own Types

    Did you know that you can compile any .NET source code on the fly and use this to create your own types? Here is an example illustrating how to create a new type from c# code that has both static and dynamic methods: $source = @ ' public class Calculator...
  • Accessing Function Parameters by Type

    Adding parameters to your functions is fairly easy. You can add a param() block and specify the parameters. But what if you wanted to assign values to different parameters based solely on type? Let's say you want a function that accepts both numbers...
  • Store Pictures in User Accounts

    Have you ever wondered how PowerShell would store a jpeg picture into your Active Directory user account? With the help of some low-level .NET calls, it's doable. You should simply adjust the path to your picture and the LDAP path to your user account...
  • Grouping with Script Blocks

    Group-Object creates groups based on object properties. For example, you could group processes by company or folder listings by extension: Dir $env:windir | Group-Object Extension Get-Process | Group-Object Company If the object has no property that reflects...
  • Accessing WMI Instances in One Line

    While you can use Get-WMIObject to query for WMI objects and then select the ones you are really after, you can also cast a WMI object path to a WMI object and get to that instance immediately. For example, the following accesses the WMI object representing...
  • Changing File/Folder Creation Date

    You should use this approach if you need to change the creation time of a folder or file after you have created it: Get-Childitem c:\testfolder | Foreach-Object { $_ . CreationTime = '1/1/1972 10:35' } It will change creation time of c:\testfolder...
  • Accessing Object Properties

    Objects store information in various properties. There are two approaches if you would like to get to the content of a given property. One is commonly used among developers, the other one among admins. Both will get you the same result. Have a look: #...
1 2 Next >
Copyright 2012 PowerShell.com. All rights reserved.