August 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
  • View Object Inheritance

    A hidden object property called "PSTypeNames" will tell you the object type as well as the inherited chain: ( Get-WMIObject Win32_BIOS). PSTypeNames In contrast to GetType(), this property will work for all objects, including COM objects. The...
  • Using Scripts to Validate Input

    For tricky validation checks, you should use arbitrary PowerShell code to validate. The function Copy-OldFiles will only accept files (no folders) and those that are older (in days) than specified in -Days: function Copy-OldFiles { param ( $Days = 30...
  • Restrict Input to Numeric Ranges

    Let's say you'd like to set the PowerShell console cursor size. This size must be a number between 0 and 100. The following template will validate that the user cannot specify an argument outside the allowed range: function Set-CursorSize { param...
  • Converting Object Types

    Once you know the name of an object type, you can use that type for conversion. The next line converts a string into a date-time type: [ DateTime ] '4.5.2010' You should note that conversion uses the culture-neutral date format which happens to...
  • Finding Object Types with Powershell

    Anything in PowerShell is an object. You can use GetType() to get the object type: 'Hallo' . GetType (). FullName (4). GetType (). FullName (2.6). GetType (). FullName ( Get-Date ). GetType (). FullName ReTweet this Tip!
  • Validate Input Using Regular Expressions

    Function parameters can be validated using standard regular expressions. For example, the next template function accepts only valid Knowledge Base article numbers beginning with "KB" and a six- digit number: function Get-KnowledgeBaseArticle...
  • Limiting String Input Length

    If a function parameter should receive a string of a given length only, you should use the following validation attribute. In the example, it limits filenames to eight characters: function Get-FileName { param ( [ValidateLength(1,8)] [ String ] $FileName...
  • Limiting Number of Arguments

    Function parameters can receive multiple values when they accept arrays. You should use this template to limit the number of values acceptable: function Add-Users { param ( [ValidateCount(1,3)] [ String []] $UserName ) $UserName - ForEach-Object { "Adding...
  • Defining Alias Properties

    Your functions can have properties with built-in alias names. The user can then either use the descriptive "long" name or its short and convenient alias name: function Get-BIOSInfo { param ( [Alias( "CN" )] $ComputerName = "."...
  • Validate Set of Inputs

    If you need to limit a function parameter to only a set of allowed choices, you should use the next example: function Get - 24hEventLogEntries { param ( [ String ] [ValidateSet( 'System' , 'Application' )] $LogName = 'System' ...
  • Finding Cmdlets With a Given Parameter

    Finding cmdlets by name is easy: Get-Command * service * -commandType Cmdlet But how can you list all cmdlets that support a given parameter? If you’d like to see all cmdlets with a -List parameter? The easiest way is to use Get-Help with the parameter...
  • Removing All Internet Explorer Cookies

    You'd probably like to regularly clean your machine and remove all cookies set by Web sites you may have visited? Here is how: Dir ([ Environment ]:: GetFolderPath ( "Cookies" )) | del -whatif You will want to make sure you remove the -whatif...
  • Listing Internet Explorer Cookies

    Have you ever wanted to find out who stored your information while you were surfing the Web? Check out your cookies folder: Dir ([ Environment ]:: GetFolderPath ( "Cookies" )) You should try this to open that folder in your Explorer: Explorer...
  • Remove Recents Folder

    Windows uses the special recents folder to remember which files you have opened. You can have a look to check what Windows has stored: Dir ([ Environment ]:: GetFolderPath ( "Recent" )) To get rid of that information, delete the folder content...
  • Bulk-Changing File Extensions

    Changing file extensions can be a quick one-line operation. The next line renames all ps1 PowerShell script files found in your user profile and renames them to the new extension "old.ps1." This way, they stay PowerShell scripts, but get an...
1 2 Next >
Copyright 2012 PowerShell.com. All rights reserved.