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
  • Getting Alphabetical Listings

    Unfortunately, PowerShells special ".." operator only supports numeric ranges: 1..10 You can use type conversion to get a range of letters: $OFS = "," [ string ][ char []](65..90) This will then create a comma-separated list of letters...
  • Counting Special Characters

    Type conversion can help you count special characters in a text. For example, if you'd like to find out the number of tab characters in a text, you should do this: @([ Byte []][ char []] $text -eq 9). Count This line will convert the characters in...
  • Chaining Type Conversions

    In PowerShell, you can do multiple sequential type conversions. For example, you should first convert the string into a character array and then into the byte array to split a string into a byte array: [ Byte []][ Char []] "Hello World!" Now...
  • Accessing Event Logs via Conversion

    You will find that type conversion can do amazing things. For example, the next line accesses a system log by converting the log name to an EventLog object: [ System.Diagnostics.EventLog ] "System" ReTweet this Tip!
  • Finding WMI Instance Path Names

    In a previous tip, you learned about how to access WMI instances directly using their individual instance path. Here is how you can find that path for arbitrary WMI objects: Get-WMIObject Win32_Share | Select-Object __Path You can simply retrieve WMI...
  • Accessing WMI Instances Directly

    If you know the path to a WMI instance, you can access it directly by converting the WMI path to a WMI object: [ wmi ] 'Win32_Service.Name="W32Time"' [ wmi ] 'Win32_Logicaldisk="C:"' You can also specify the full WMI...
  • Type Accelerators

    PowerShell has a few shortcuts for popular .NET types like [WMI], [ADSI] or [Int]. You should read the FullName property if you'd like to know the underlying full type name: [ WMI ]. FullName ReTweet this Tip!
  • Comparing Versions

    When you compare version strings, PowerShell will use alphanumeric algorithms, which may lead to confusing results: '' -gt '' True You should convert the strings to a System.Version type to compare version strings right...
  • 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...
« First ... < Previous 72 73 74 75 76 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.