November 2012 - 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
  • "Count" Available in PowerShell 3.0

    Finally, the property Count is available on all objects in PowerShell 3.0. This solves a great problem because it allows you to count results even if they are not wrapped in an array. Have a look: PS > ( Get-ChildItem $env:windir \ * . txt ) . Count...
  • Removing Empty Object Properties (All Versions)

    In a previous tip we showed how you can remove all empty properties from an object and also sort its properties alphabetically. The code required PowerShell 3.0 features. Here is a variation that runs on all PowerShell versions. Again, it takes a BIOS...
  • Removing Empty Object Properties

    Objects hold a lot of information and often, properties can also have null values. To reduce an object to only those properties that actually have a value, you can convert the object into a hash table and remove all empty properties, then turn the hash...
  • Controlling Object Property Display

    When you create functions that return custom objects, there is no way for you to declare which functions should display by default. PowerShell always displays all properties, and when there are more than 4, you get a list display, else a table: function...
  • Logging Input Commands

    If you'd like to maintain a log file with all the commands you entered interactively - in the PowerShell console as well as in the ISE editor - here is an easy way: Simply redefine the built-in prompt function. It is responsible for writing the prompt...
  • Creating Objects in PowerShell 3.0 (Fast and Easy)

    In PowerShell 3.0, you can cast a hash table to a PSCustomObject type to easily generate your own objects: $content = @ { Name = "Weltner" FirstName = ' Tobias ' id = 123 } [ PSCustomObject ] $content The result is an object : Name FirstName...
  • Protecting Functions

    To prevent a function to be redefined or overwritten, you can write-protect it: function Test-Function { ' Hello World! ' } Set-Item -Path function : Test - function -Options ReadOnly From this point, your function cannot be overwritten. However...
  • Creating New Objects the JSON way

    There are numerous ways how you can create new objects that you may use to return results from your functions. One way is using JSON, a very simple description language. It is fully supported in PowerShell 3.0. Have a look: PS > $content = ' {"Name"...
  • WhatIf-Support Without Propagation

    You can enable the -WhatIf and -Confirm parameters in your functions too, and control which parts of your code get skipped if the user specifies -WhatIf or denies execution with -Confirm : function Test-WhatIf { [ CmdletBinding ( SupportsShouldProcess...
  • Finding Enumeration Data Types

    In a previous tip you learned that assigning an enumeration data type to a function parameter automatically enables argument completion in PowerShell 3.0. This just takes one line of code, but the hard part is to find a suitable enumeration data type...
  • Using Enumeration Types for Parameter IntelliSense

    In a previous tip you learned how decorating function parameters with a ValidateSet attribute would allow the ISE editor to display intelliSense help for the parameter arguments. As an alternative, you can assign an enumeration data type to a parameter...
  • Rich IntelliSense for Function Arguments

    To take advantage of the new PowerShell 3.0 argument completion, make sure you're adding ValidateSet attribute to your function parameters (where appropriate). In previous versions of PowerShell, ValidateSet made sure the user could enter only values...
  • Get-WmiObject Becomes Obsolete

    In PowerShell 3.0, while you still can use the powerful Get-WmiObject cmdlet, it is slowly becoming replaced by the family of CIM cmdlets. If you use Get-WmiObject to query for data, you can easily switch to Get-CimInstance . Both work very similar. The...
  • Finding Built-In Cmdlets

    In times where cmdlets can originate from all kinds of modules, it sometimes becomes important to find out which cmdlets are truly built into PowerShell and which represent external dependencies. One way of getting a list of built-in cmdlets is to temporarily...
  • Providing "Static" IntelliSense for Your Functions

    To get rich IntelliSense in PowerShell ISE 3.0, you should start adding the OutputType attribute to your functions. If you do, then ISE is able to provide IntelliSense inside your code without the need to actually have real values in your variables. Here...
1 2 Next >
Copyright 2012 PowerShell.com. All rights reserved.