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

Power Tips

Power Tips

Syndication

  • 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...
  • Use Mandatory Parameters

    Mandatory parameters are cool: you can submit values to them for automated solutions, and you can omit them and get prompted interactively. Here is an example: function Get-Birthday { param ( [ DateTime ] [ Parameter ( Mandatory = $true )] $Date ) $realDate...
  • Accept User Input with Validator

    Of course you can use Read-Host to ask a user for some input, or use mandatory parameters. They all are kind of ugly, and have next to no validation. Here is a function that makes things easier: function Get-UserInput { param ( [ Parameter ( Mandatory...
  • Keeping Track of Installed Modules

    When you start using PowerShellGet to download modules from the PowerShell Gallery ( www.powershellgallery.com ) or your own NuGet repositories, you may want to keep track of the modules you installed. Here is a one-liner that shows you what was installed...
  • Tailing Log Files

    You can ask PowerShell to monitor a text-based log file and output any changes. It is a one-liner: Get-Content c:\Windows\WindowsUpdate.log -Wait -Tail 0 Typically, Get-Content would only read the text content from the given file. -Wait instructs PowerShell...
  • Checking Paths and Files for Illegal Characters

    In the previous tip we illustrated how you can get a list of characters that are illegal in paths or file names. However, what would be the best and easiest way to check whether any of these characters actually existed in a given path or file name? Simply...
  • Finding Invalid File and Path Characters

    When dealing with files and folders, some characters are considered illegal. Here is an easy way to get a list of the characters that Windows declared as illegal: $invalidPath = -join [ System.IO.Path ] :: GetInvalidPathChars () $invalidPath $invalidFile...
  • Operate Your Own PowerShell Gallery

    With PowerShellGet, you can easily share PowerShell code. Go to www.powershellgallery.com and download the MSI installer to install PowerShellGet if you are not running the latest PowerShell 5.0 version. In the previous tip, we showed how to search for...
  • Identifying Special Characters

    If you must ensure that a string contains only a given set of characters, try this: $text = ' tobias.weltner ' $hasOtherCharacters = $text -cmatch ' [^a-zA-Z] ' "$text has illegal characters? $hasOtherCharacters" It is really...
  • Inserting Text into String

    Use Insert() to insert new text into an existing string at a given position. Here is an example: $text = "Server failed" 1 . .100 | ForEach-Object { $text . Insert ( 6 , $_ ) } It produces a list of servers with an increment. Of course, this...
  • Dealing With Long File Names

    The Windows file system often has issues with file paths longer than 256 characters. Provided you followed our tips and installed PowerShellGet, you can easily download a PowerShell module with new cmdlets that help overcome this limit: PS> Install...
  • Colorizing PowerShell Console

    In PowerShell 5.0, when you enter code into the PowerShell console, the tokens get colorized, much similar to the PowerShell ISE interactive console. This colorization is done by a module named "PSReadLine" that ships with PowerShell 5.0. If...
  • Check PowerShell Gallery Code

    In the previous tip we introduced the PowerShell Gallery and showed how to download content (scripts and modules). Since the PowerShell Gallery is a public repository, and anyone can upload code, you should execise caution. Use Install-Module or Install...
  • Using PowerShell Gallery

    The PowerShell Gallery is a public repository for PowerShell scripts and modules. It is entirely managed by cmdlets. Before you can use the PowerShell Gallery to download new scripts and modules, or to share your own with others, you need the PowerShellGet...
1 2 3 4 5 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.