Power Tips

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Powershell Plus


13 Free SQL Tools from Idera


Free Ebook: Mastering Powershell

Archives

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
  • Extracting Text Information

    Tracert.exe tells you the hops your data packages need to take to get to a given host. Here is an example: PS C:\> tracert www.microsoft.com Tracing route to e10088.dspb.akamaiedge.net [2.16.194.227] over a maximum of 30 hops: 1 8 ms 8 ms 9 ms 1st...
  • Bulk-Convert to String

    Sometimes, commands and methods do not return exactly what you are after. If you, for example, wanted to get the assigned IP addresses for a hostname, you could try this: [ System.Net.DNS ] :: GetHostByName ( ' microsoft.com ' ) . AddressList...
  • Create Local Admin Accounts

    There is no module available for all PowerShell versions that allows to administer local user accounts. Here is a chunk of sample code that illustrates how you nevertheless can automate local account management. The code creates a new local Administrator...
  • Loops and Arrays (and some surprises)

    Frequently, loops are used to retrieve (or generate) data, then save it to a variable. There can be tremendous performance differences though. To generate 10.000 random numbers, for example, you might look into something like this: $numbers = @ () for...
  • Finding Top 3 (of anything)

    Ever wanted to know who are the three worst sources for errors in your System event log? Here is a simple approach that yields the source names with the most errors: Get-EventLog -LogName System -EntryType Error | Group-Object -Property Source | Sort...
  • Converting Fixed-Width Text Data

    PowerShell can turn fixed-width tabular data easily into rich objects. First, define the column widths. Then, have PowerShell inject a delimiter at these positions. Finally, ConvertFrom-Csv can split the text lines and turn the text into rich objects...
  • Exclude Properties

    With Select-Object, you typically include the object properties you want to receive. You can do it the other way around, too, though. Here is an example that includes all properties except some unwanted ones: #requires -Version 2 # get the latest 20 errors...
  • Creating Temporary Passwords

    A good temporary password should be complex, yet unambiguous. So it should contain a mixture of upper and lower case plus special characters, but it should avoid conflicting characters such as "o", "O", and "0". Here is sample...
  • Beeping PowerShell

    Your colleague went to lunch again without properly locking his computer? Then this is your chance to run this code on his PowerShell: $powershell = [ PowerShell ] :: Create () $code = { while ( $true ) { Start-Sleep -Milliseconds 3000 [ Console ] ::...
  • Controlling Return Values

    In the previous tip we explained why functions can have multiple return values, and that anything you leave behind will be added to the return values. If you'd like PowerShell to behave like a classic scripting language, and exclusively set the return...
  • Understanding Multiple Return Values

    PowerShell has an unusual way of dealing with return values. Anything you left behind will be added to the return values. The statement "return" does not exclusively define a return value. This is why the following function has multiple return...
  • Pretty-Printing Error Objects

    Whenever you deal with error objects, you may want to use the following PowerShell function: Get-ErrorInfo. It accepts any number of error records, and turns them into easily usable error information objects: function Get-ErrorInfo { param ( [ System...
  • Automatically Updating Modules

    Modules installed via PowerShellGet can be updated automatically to the latest version. You would need to run PowerShell with full Administrator privileges to update modules that were installed for all users. Please take the following code just as an...
  • Keeping Your Modules Up-To-Date

    Once you start downloading modules from the PowerShell Gallery (or via PowerShellGet in general), you get version control automatically. You can always review the modules that were installed, and compare their versions with the versions available in the...
  • Better Prompting for Mandatory Parameters

    When you do not submit an argument to a mandatory parameter, PowerShell prompts you for a value. You don't have much control over how this is done, and most find it ugly. To use your own prompting, make the parameter optional, and provide code that...
< Previous 1 2 3 4 5 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.