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

Power Tips

Power Tips

Syndication

  • Converting Arrays to Strings in CSV Exports

    When you export objects to CSV--for example to display them in Excel--arrays won't be output correctly. Here is a simple way that converts arrays to strings prior to outputting them to CSV: #requires -Version 2 $Path = " $env:temp\report.csv...
  • Exploring PowerShell Automatic Variables

    Here is an easy way to get a list of all currently defined variables, their values, and their purpose. #requires -Version 2 Get-Variable | Select-Object -Property Name , Description , Value | Out-GridView If you'd like to set a description for your...
  • Invoking Code Repeatedly

    Sometimes you might want to run some command multiple times until it runs successfully. Here is a function that shows a way to do this: #requires -Version 2 function Invoke-CodeRepeatedly { param ( [ Parameter ( Mandatory = $true )] $ScriptBlock , $RepeatCount...
  • Saving Persistent Data

    Sometimes a script needs to save information in a persistent way. Maybe you have a list of computers that you'd want to contact, but only some are online. Then your script should report which computers could be contacted, so when you run the script...
  • Use WMI the Modern Way!

    WMI is a powerful technique to find out information about local or remote computers, and you may have used Get-WmiObject before to do so (if not, you may want to google for this cmdlet). In PowerShell 3.0, a new cmdlet named Get-CimInstance was added...
  • Using .NET Types Directly

    Cmdlets contain pure .NET code, so thanks to cmdlets, you do not need to directly touch .NET code. You can, however. Here are a number of sample calls that illustrate how .NET methods can be accessed: #requires -Version 2 [ System.Convert ] :: ToString...
  • Using Workflows to Parallelize Code

    If you want to execute more than one thing at once, there are many ways to implement this in PowerShell. One may be the use of workflows. They were introduced in PowerShell 3.0: #requires -Version 3 workflow Test-ParallelForeach { param ( [ String []...
  • Decorate Scripts with #requires Statements

    PowerShell supports a number of #requires statements. Technically they are comments but PowerShell checks the requirements, and if they are not met, it won't run a script. In addition, #requires statements tell you quickly what a script needs to run...
  • Do Not Mix Different Objects!

    If you do output completely different objects, you may lose information. Take a look at this example: #requires -Version 2 $hash = @ { Name = ' PowerShell Conference EU ' Date = ' April 20, 2016 ' City = ' Hannover ' URL = '...
  • Getting an Excuse

    Here is a quick way of getting a good excuse - provided you have Internet access: #requires -Version 3 function Get-Excuse { $url = ' http://pages.cs.wisc.edu/~ballard/bofh/bofhserver.pl ' $ProgressPreference = ' SilentlyContinue ' $page...
  • Who is Listening? (Part 2)

    If you run at least Windows 8 or Windows Server 2012, you can use Get-NetTcpConnection to find out which network ports are in use, and who is listening on these ports. The script below not only lists the ports in use but also the processes that do the...
  • Who Is Listening? (Part 1)

    The good oldfashioned netstat.exe can tell you the ports that applications listen on. The result is plain-text, though. PowerShell can use regular expressions though to split the text into CSV data, and ConvertFrom-Csv can then turn the text into real...
  • Sending Objects to Notepad

    In a previous tip we showed how you can send text to a fresh Notepad instance. Today, you get an enhanced version of Out-Notepad: you can pipe anything to Notepad now. If it is not a string, Out-Notepad uses the internal PowerShell ETS to convert it to...
  • Send Text to Notepad

    Notepad can be used to display text results. Typically, you would need to save text results to file, then have Notepad open that file. There is a better way, though: launch an empty Notepad, and send the text via Windows messages directly to the untitled...
  • Magic Underscore Variable

    Here is a very special (and very underdocumented) way to use PowerShell parameters. Have a look at this function: #requires -Version 2 function Test-DollarUnderscore { param ( [ Parameter ( Mandatory = $true , ValueFromPipelineByPropertyName = $true ...
« First ... < Previous 3 4 5 6 7 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.