PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Forum experts available to answer your PowerShell questions

Power Tips

Power Tips

Syndication

  • Logging What a Script Does

    All PowerShell versions You probably know that in a PowerShell console (not the ISE editor), you can turn on logging: PS> Start-Transcript This will log all entered commands and all command results to a file. Unfortunately it is of limited use when...
  • A Fun Beeping Prompt

    All PowerShell versions If your computer has a sound card, here is a code snippet that will drive your colleagues nuts: function prompt { 1 . .3 | ForEach-Object { $frequency = Get-Random -Minimum 400 -Maximum 10000 $duration = Get-Random -Minimum 100...
  • Returning More Than One Value

    All PowerShell versions If a PowerShell function needs to return more than one value, best practice is to return objects, and store the information in separate object properties. Here is a simple and charming alternative for some scenarios. Simply return...
  • Edit Network “hosts” File

    All PowerShell versions If you find yourself editing the “hosts” file regularly, then it may be tedious to manually open the file in an elevated instance of the Notepad. Since this file can only be edited by Administrators, a normal Notepad...
  • Watch Out With UNC Paths!

    All PowerShell versions Many cmdlets can deal with UNC paths, but using UNC paths can produce flaky scripts. Take a look at this: PS> Test-Path -Path \\127.0.0.1\c$ True It returns true, the UNC path exists. Now change the current drive to a non-Filesystem...
  • Finding AD Users

    All PowerShell versions Searching the AD can be done with simple calls provided you are logged on an Active Directory domain. In a previous tip we illustrated the basic script. Here is an extension that allows you to define a search root (starting point...
  • Finding and Dumping Registry Key Paths

    All PowerShell versions In a previous tip we illustrated how to convert an internal PowerShell path format to a real path. Here is a use case. This code recursively searches through HKEY_CURRENT_USER and dumps all Registry keys that contain the word “powershell”...
  • Correcting PowerShell Paths

    All PowerShell versions Occasionally, you might stumble across strange path formats like this one: Microsoft.PowerShell.Core \ FileSystem :: C:\windows\explorer.exe This is a full PowerShell path name which includes the module name and provider name that...
  • Case-Correct Name Lists

    All PowerShell versions Let’s assume it’s your job to update a list of names. Here is an approach that will make sure that only first letter in a name is capitalized. This approach works with double-names as well: $names = ' some-wILD...
  • Hibernate System

    All PowerShell versions Here is a simple system call that will hibernate a system (provided of course that hibernation is enabled): function Start-Hibernation { rundll32.exe PowrProf.dll , SetSuspendState 0 , 1 , 0 } Note that this call is case-sensitive...
  • Recursing a Given Depth

    PowerShell 3.0 and newer When you use Get-ChildItem to list folder content, you can add the –Recurse parameter to dive into all subfolders. However, this won’t let you control the nesting depth. Get-ChildItem will now search in all subfolders...
  • Managing Printers Low-Level

    All PowerShell versions Recent Windows operating systems like Windows 8 and Server 2012 come with great printing support, but if you run older Windows versions, then this call may help: PS> rundll32.exe PRINTUI.DLL,PrintUIEntry PS> Note that this...
  • Aliases Can Be Dangerous

    All PowerShell versions Aliases enjoy the highest priority among executable commands in PowerShell, so if you have ambiguous commands, PowerShell always picks the alias. This can be dangerous: if you allow others to change your PowerShell environment...
  • Converting Special Characters, Part 2

    All PowerShell versions In a previous tip we illustrated how you can replace special characters in a text. Here is another approach that may be a bit slower but is easier to maintain. It also features a case-sensitive hash table: function ConvertTo -...
  • Converting Special Characters, Part 1

    All PowerShell versions Sometimes it becomes necessary to replace special characters with other characters. Here is a simple function that does the trick: function ConvertTo - PrettyText ( $Text ) { $Text . Replace ( ' ü ' , ' ue '...
« First ... < Previous 3 4 5 6 7 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.