Blog - Post List
  • Power Tips: Accessing Free PowerShell Gallery (Part 2)

    PowerShellGet is a new free extension for PowerShell that enables you to download and install free extensions and commands. It comes with PowerShell 5, and in the previous tip we explained how you can update it to the latest version.

    If you are using an older PowerShell version, the best advice is to update PowerShell. If you can’t do this, you can install the PowerShellGet module via a free MSI package here: https…

    • 21 May 2018
  • Power Tips: Accessing Free PowerShell Gallery (Part 1)

    PowerShell is an extensible framework, and there are tons of free and very useful command extensions available on the PowerShell Gallery (www.powershellgallery.com). We’ll be examining the vast power of this gallery in a couple of tip posts.

    To download and install any extension from PowerShellGallery, you need the PowerShellGet module which provides you with the commands to browse, download, install, update, and…

    • 18 May 2018
  • Power Tips: Printing Tables from PowerShell (using WPF)

    Whenever you would like to display, print, or save as PDF in a tabular form, WPF (Windows Presentation Foundation) may be a good way. Originally, WPF was created to define user interfaces, but you can as well use it to define tables, fill in the data, and print or save them.

    Don’t be put off by the amount of code. PowerShell is basically defining the table in an object-oriented way.

    The example code below creates…

    • 17 May 2018
  • Power Tips: Creating PowerShell Command Cheat Sheets (Part 4)

    In the previous tip, we created cheat sheets for PowerShell commands. We used command names and their synopsis to create cheat sheets like this:

     
    PS> Get-Command -Module "PrintManagement"| 
      Get-Help | 
      Select-Object -Property Name, Synopsis
    
    Name                   Synopsis                                             
    ----                   --------                                             
    Add-Printer        …
    • 16 May 2018
  • Power Tips: Creating PowerShell Command Cheat Sheets (Part 3)

    In the previous tip, we created cheat sheets for PowerShell commands and turned them into HTML reports that could be opened in a browser, and printed. That worked well, but the output page wasn’t very fancy. With just a few HTML styles, your command list is ready for prime-time, and you can use the technique in this example to “beautify” any other HTML table you may create from PowerShell using ConvertTo-Html:…

    • 15 May 2018
  • Power Tips: Creating PowerShell Command Cheat Sheets (Part 2)

    In the previous tip, we created cheat sheets for PowerShell commands, and as a reminder, this line would create a sheet for network commands (provided you have access to the NetAdapter module that was browsed in this example):

     
    PS> Get-Command -Module NetAdapter | Get-Help | Select-Object -Property Name, Synopsis
    
    Name                                            Synopsis                       
    ----                   …
    • 14 May 2018
  • Power Tips: Creating PowerShell Command Cheat Sheets (Part 1)

    Here is a clever trick to create cheat sheets for your favorite PowerShell commands.

    Any PowerShell command ships via a module, so if you have a PowerShell command that is useful to you, you might want to list all the other (and possibly related) commands that ship in the same module. This line shows how to find the module name for any given command, and lists the module name that ships the Get-WmiObject cmdlet:

     
    PS…
    • 11 May 2018
  • Power Tips: Quickly Open PowerShell via Windows 10 Start Menu

    In Windows 10, PowerShell can be launched via WIN+X (or by right-clicking the start menu icon). If your Windows 10 offers to open the outdated cmd.exe instead, it’s time for a quick settings change.

    Click the start menu icon (or press WIN). In the start menu, click the “gear” icon to open Windows Settings. You now see a window full of symbols called “Windows Settings”, and a text box on top labelled “Find a setting…

    • 10 May 2018
  • Power Tips: Test AD User Exists

    If you’d need to find out whether a given Active Directory user exists, and provided you have installed the ActiveDirectory PowerShell module which is part of RSAT (Remote Server Administration Toolkit), here is a clever way:

    function Test-UserExists
    {
        param
        (
            [Parameter(Mandatory)]
            [string]
            $SAMAccountName 
        )
     
        @(Get-ADUser -LDAPFilter "(samaccountname=$SAMAccountName)"…
    • 9 May 2018
  • Power Tips: WMI Quick Primer (Part 4)

    Typically, WMI objects contain properties with valuable information. This line would retrieve all information about all running Notepad instances (make sure you launch Notepad before running this):

    Get-WmiObject -Class Win32_Process -Filter 'Name LIKE "%notepad%"'
    

    Likewise, this would use Get-CimInstance to get the same information:

    Get-CimInstance -Class Win32_Process -Filter 'Name LIKE "%notepad…
    • 8 May 2018
  • Power Tips: WMI Quick Primer (Part 3)

    In the previous tip we explained why Get-CimInstance may have advantages over the older Get-WmiObject cmdlet.

    Now here is another example that illustrates just why Get-CimInstance may be much faster than Get-WmiObject.

    When you need to query multiple WMI classes from a remote machine, for example because you are building an inventory report, Get-WmiObject needs to connect and disconnect each time you run the cmdlet. Get…

    • 7 May 2018
  • Power Tips: WMI Quick Primer (Part 2)

    There are two cmdlets you can use to retrieve WMI data: the older Get-WmiObject cmdlet, and the more modern Get-CimInstance cmdlet. When used locally, they both behave very similarly. When used remotely, however, the differences can be drastic.

    Here are two example calls that both retrieve information about file shares from a remote system (make sure you adjust the computer name):

    Get-WmiObject -Class Win32_Share -Co…
    • 4 May 2018
  • Power Tips: WMI Quick Primer (Part 1)

    WMI is an awesome information source for admins. All you need is the name of a WMI class that represents something you find interesting. The easiest way to find a valid WMI class name is to search for it.

    This returns all classes with “Share” in their name:

    Get-WmiObject -Class *share* -List
    

    Next, use Get-WmiObject to retrieve all instances of a class:

    Get-WmiObject -Class win32_share 
    

    Don’t forget to pipe…

    • 3 May 2018
  • Power Tips: Listing Properties with Values (Part 3)

    When you want to inspect individual objects and their properties, piping an object to Out-GridView is of limited use: the grid view window will display a single (very long) line with the properties. Try this and see for yourself:

     
    PS> Get-Process -Id $pid | Select-Object -Property * | Out-GridView  
     

    We have used the below function before to sort out all properties that have no value. But the function can do more…

    • 2 May 2018
  • Power Tips: Listing Properties with Values (Part 2)

    In the previous tip we introduced a new function called Remove-EmptyProperty that removes properties that have no value. Let’s expand it a bit so that object properties are alphabetically sorted:

    # Only list output fields with content
    
    function Remove-EmptyProperty  {
        param (
            [Parameter(Mandatory,ValueFromPipeline)]
                $InputObject,
                
                [Switch]
                $AsHashTable
    • 1 May 2018
  • Power Tips: Listing Properties with Values (Part 1)

    Objects are filled with rich information, yet objects may contain empty properties. This is especially true for objects retrieved from Active Directory.

    Here is a useful function called Remove-EmptyProperty which accepts arbitrary objects and removes all empty properties:

    # Only list output fields with content
    
    function Remove-EmptyProperty  {
        param (
            [Parameter(Mandatory,ValueFromPipeline)]
                $…
    • 30 Apr 2018
  • Power Tips: Enabling Remoting

    There are many ways how cmdlets can get remote information from another computer. Here are just a few:

    # try and connect to this computer
    # (adjust it to a valid name in your network)
    $destinationServer = "SERVER12"
    
    # PowerShell remoting
    $result1 = Invoke-Command { Get-Service } -ComputerName $destinationServer
    
    # built-in
    $result2 = Get-Service -ComputerName $destinationServer
    $result3 = Get-Process -Comp…
    • 27 Apr 2018
  • Power Tips: Using ConvertFrom-ErrorRecord in try/catch

    In the previous tip we created a function called ConvertFrom-ErrorRecord that makes it easy to retrieve all relevant error information from PowerShell’s ErrorRecord objects.

    You can use this function inside catch clauses as well. Just be sure you ran the function below:

    function ConvertFrom-ErrorRecord
    {
      [CmdletBinding(DefaultParameterSetName="ErrorRecord")]
      param
      (
        [Management.Automation.ErrorRecord]…
    • 26 Apr 2018
  • Power Tips: Converting Error Records

    Whenever PowerShell raises an error, an error record is written to $error which is an array storing the last errors that occurred.

    You can try and manually extract relevant error information from ErrorRecord objects, or you can use the function below:

    function ConvertFrom-ErrorRecord
    {
      [CmdletBinding(DefaultParameterSetName="ErrorRecord")]
      param
      (
        [Management.Automation.ErrorRecord]
        [Parameter(Mandatory
    • 25 Apr 2018
  • Power Tips: Downloading Information from Internet (Part 8)

    In the previous tip we illustrated how to download files from the internet using Invoke-WebRequest. However, this works for HTTP addresses only. Once you start using HTTPS addresses, it fails:

    $url = "https://github.com/PowerShellConferenceEU/2018/raw/master/Agenda_psconfeu_2018.pdf"
    $destination = "$home\agenda.pdf"
    
    Invoke-WebRequest -Uri $url -OutFile $destination -UseBasicParsing
    Invoke-Item -…
    • 24 Apr 2018
  • Power Tips: Downloading Information from Internet (Part 7)

    In this part of this mini-series, we are showing you how Invoke-WebRequest can download files from the internet for you. Simply use the parameter -OutFile. This code downloads a PowerShell icon as PNG image onto your desktop:

    $url = "http://www.dotnet-lexikon.de/grafik/Lexikon/Windowspowershell.png"
    $destination = "$home\powershell.png"
    
    Invoke-WebRequest -Uri $url -OutFile $destination -UseBasicP…
    • 23 Apr 2018
  • Power Tips: Downloading Information from Internet (Part 6)

    In the previous tip we explained how you can retrieve XML data from webpages using either Invoke-WebRequest or Invoke-RestMethod. For XML data, there is also another approach which uses the built-in methods found in the XML object itself.

    This was the approach with Invoke-RestMethod:

    $url = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'
    (Invoke-RestMethod -Uri $url -UseBasicParsing).Envelope.C…
    • 20 Apr 2018
  • Power Tips: Downloading Information from Internet (Part 5)

    In the previous tip we illustrated how Invoke-WebRequest can be used to download JSON or XML data from a web page. This example downloads the psconf.eu agenda in JSON format:

    $page = Invoke-WebRequest -Uri powershell.beer -UseBasicParsing 
    $($page.Content | ConvertFrom-Json) | Out-GridView
    

    And this example downloads currency exchange rates in XML format:

    $url = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref…
    • 19 Apr 2018
  • Power Tips: Downloading Information from Internet (Part 4)

    In the previous tip we explained how you can use Invoke-WebRequest to download data from webpages, and for example retrieve excuses from a webpage that serves random excuses. However, when you try this, you might get back always the same excuse (or data).

    $url = 'http://pages.cs.wisc.edu/~ballard/bofh/bofhserver.pl'
    $page = Invoke-WebRequest -Uri $url -UseBasicParsing
    $content = $page.Content
    
    $pattern = '…
    • 18 Apr 2018
  • Power Tips: Downloading Information from Internet (Part 3)

    In previous tips, we showed how to use Invoke-WebRequest to download data from webpages, and process data delivered in JSON or XML format. Most webpages contain plain HTML data, however. You can use regular expressions to pick information from plain HTML.

    This is how you get to webpage content:

    $url = 'http://pages.cs.wisc.edu/~ballard/bofh/bofhserver.pl'
    $page = Invoke-WebRequest -Uri $url -UseBasicParsing
    $…
    • 17 Apr 2018