December 2011 - 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
  • Spying on Parameters

    Your own PowerShell functions can have the same sophisticated parameters, parameter types and parameter sets that you know from cmdlets. However, it is not always obvious how to construct the param() block appropriately. A clever way is to spy on cmdlets...
  • Removing Shares (Remotely, Too)

    In a previous tip you learned how you can create ad-hoc shares using WMI. These shares persist until you remove them again. So if you'd like to clean up behind you, here is a one-liner that removes a share locally or remote: PS > ([ wmi ] '...
  • Creating Shares Remotely

    Let's assume you need to access another machine's file system but there is no network share available. Provided you have local administrator privileges and WMI remoting is allowed in your Firewall, here is a one-liner that adds another share remotely...
  • Opening MsgBoxes

    Need a quick message box to display something or ask a question? Fortunately, PowerShell can access old COM components. Here's a line that creates a MsgBox for 5 seconds. If the user does not make a choice within that time, it returns -1: a perfect...
  • Creating a "Better" More

    In a previous tip you learned that using "more" to paginate output can be dangerous, and instead you should use Out-Host -Paging. To "update" more.com and make it behave like Out-Host with the -Paging parameter set, use a proxy function...
  • "More" Can Be Dangerous - Use Better Alternative

    You might know the more.com tool: when you pipe output to more.com, the output is displayed page by page: PS > Get-EventLog -LogName System | more However, "more" can be dangerous as you see here. You will not get any results for a long time...
  • Creating Your Own Get-Driver Tool

    Some code snippets are really valuable, so you should turn them into functions to keep around as new PowerShell commands. Here's a sample function: function Get-Driver { param ( $Keyword = ' * ' ) $col1 = @ { Name = ' File Name ' ;...
  • Finding More Driver Information

    In a previous tip you learned how you can convert raw CSV data from a console application such as driverquery.exe into real PS objects. Let's play some more. Since the CSV data only returns the complete path to a driver (which is too long to display...
  • Finding Driver Information

    driverquery.exe returns all kinds of information about installed drivers, but the information seems a bit useless at first: PS > driverquery.exe /V Module Name Display Name Description Driver Type Start Mode State Status Accept Stop Accept Pause Paged...
  • Finding Standard Parameter Names

    In a previous tip, we suggested you to use standard parameter names for your own functions. To get a feeling for what the parameter names are that built-in cmdlets use, here is some code that creates a list of them: PS > Get-Command -CommandType Cmdlet...
  • Best Practice for PowerShell Functions

    This is a best-practice message: when you create your own function, here are some things you should consider: - Function name: use cmdlet naming syntax (Verb-Noun), and for verbs, stick to the list of approved verbs. For the noun part, use a meaningful...
  • Adding New Lines to Strings

    In a previous tip you learned that text arrays can easily be multiplied. The same is true for assignment operators such as +=. When you apply this operator to a string, it appends a text: PS > $text = "Hello" PS > $text += "World"...
  • Creating Multiline Strings

    You probably know what this line produces: ' Hello ' * 12 Right, you get 12 times the string you submitted. If you wanted new lines instead, a minor adjustment will do the job: , ' Hello ' * 12 The comma puts the string into an array,...
  • Enumerating Registry Keys

    To enumerate all subkeys in a Registry key, you might be using a line like this: PS > Dir HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | Select-Object -expand PSPath Microsoft.PowerShell.Core \ Registry :: HKEY_LOCAL_MACHINE \ SOFTWARE...
  • Asking for Credentials

    When you write functions that accept credentials as parameters, add a transformation attribute! This way, the user can either submit a credential object (for example, a credential supplied from Get-Credential), or simply a user name as string. The transformation...
1 2 Next >
Copyright 2012 PowerShell.com. All rights reserved.