• Creating Highspeed Ping (Part 5)

    In the previous tip we created a lightning fast new PowerShell function called Test-OnlineFast which used WMI to ping any number of computers with high performance. Today we’ll make it even more useful by adding a number of additional properties to the ping result.

    Let’s first check how Test-OnlineFast works. Here are some examples. Let’s first ping a number of computers. You can use both computer names and IP addresses…

    • 21 Feb 2018
  • Creating Highspeed Ping (Part 4)

    In the previous tip we illustrated how WMI can ping multiple computers in a very fast way. So today, let’s wrap the code into a reusable PowerShell function. It can ping one or many computers with lightning speed.

    Here’s the function code:

    function Test-OnlineFast
    {
        param
        (
            [Parameter(Mandatory)]
            [string[]]
            $ComputerName,
     
            $TimeoutMillisec = 1000
        )
        
        # convert…
    • 20 Feb 2018
  • Creating Highspeed Ping (Part 3)

    In the previous tip we illustrated how WMI can ping multiple computers in a very fast way. However, the syntax was awkward. So let’s rewrite the code to make it easier to specify the list of computers you want to ping:

    # ping the specified servers with a given timeout (milliseconds)
    $ComputerName = 'google.de','microsoft.com','r13-c00'
    $TimeoutMillisec = 1000
     
    # convert list of computers…
    • 19 Feb 2018
  • Creating Highspeed Ping (Part 2)

    In the previous tip we illustrated how WMI can ping computers with a predefined timeout. WMI can do more: it can ping multiple computers lightning fast even though the syntax is a bit awkward.

    Here is how you can ping more than one computer:

    # ping the specified servers with a given timeout (milliseconds)
    $TimeoutMillisec = 1000
    
    Get-WmiObject -Class Win32_PingStatus -Filter "(Address='microsoft.com' or Address='r13-c14…
    • 16 Feb 2018
  • Creating High-Speed Ping (Part 1)

    Pinging computers is a frequently needed task. The PowerShell cmdlets such as Test-Connection can do pings but do not have a timeout limit, so when you try and ping offline systems, it may take a long time to get a result.

    WMI can ping with timeouts. Here is how:

    $ComputerName = 'microsoft.com'
    $TimeoutMillisec = 1000
    
    Get-WmiObject -Class Win32_PingStatus -Filter "Address='$ComputerName' and timeout=$TimeoutMillisec…
    • 15 Feb 2018
  • Getting Help for PowerShell

    Provided you have downloaded the PowerShell help files, there is an easy way to get help for all kinds of PowerShell topics.

    First, make sure you downloaded the help: launch PowerShell with Administrator privileges, and run this:

    Update-Help -UICulture en-us -Force
    

    Next, check out the “about”-topics you got:

    Get-Help about_*
    

    In the PowerShell ISE, all you’d need to do is click one of the about topics listed…

    • 14 Feb 2018
  • Exploiting Select-Object

    Select-Object is a basic cmdlet that most PowerShell users use frequently. However, it has some tricks that are not well known.

    In its most basic form, Select-Object selects the properties that are visible. If you don’t use Select-Object, then PowerShell determines which properties are shown, and how they are formatted:

    Get-ChildItem -Path c:\windows\system32 -Filter *.dll
    

    If you add Select-Object, you can determine…

    • 13 Feb 2018
  • Reading RunOnce Key

    The RunOnce key in the Windows Registry stores all auto-starting executables. It may be empty. To check for auto-starting applications, try this:

    $path = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce'
    $properties = Get-ItemProperty -Path $path 
    $properties
    

    Again, this key may have no values. If it has, each auto-starting program has its own value with its own name. To read just the paths for auto-starting…

    • 12 Feb 2018
  • Creating Random Passwords

    Here is another small script to produce random passwords consisting of a defined number of capitals, letters, numbers, and special characters:

    $length = 10
    $length_small = $length - 3
    $numbers = '2,3,4,5,6,7,8,9' -split ','
    $large = 'A,B,C,D,E,F,G,H,K,L,M,N,P,R,S,T,U,V,W,X,Y,Z' -split ','
    $small = 'A,B,C,D,E,F,G,H,K,L,M,N,P,R,S,T,U,V,W,X,Y,Z'.ToLower() -split ','
    $speci…
    • 9 Feb 2018
  • Converting PowerShell to Batch

    Here is a fun PowerShell function called Convert-PowerShellToBatch. Provide it with the path to a PowerShell script, or pipe in the results from Get-ChildItem to batch-convert many scripts.

    The function creates a batch file per script. When you double-click the batch file, the PowerShell code executes.

    function Convert-PowerShellToBatch
    {
        param
        (
            [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelin…
    • 8 Feb 2018
  • Piping Files and Folders

    Let’s assume you want to create a function that accepts a file path. There are numerous tasks that can be done with files. You may want to copy files, archive them, turn them to hidden files, or whatever else comes to mind. We are not focused on what you do with files here. We want to look at how your PowerShell function can accept files from a user.

    So you’d come up with a function like this:

    function Process…
    • 7 Feb 2018
  • Simple WMI Browser

    WMI is a powerful information repository – if you know the names of WMI classes:

    Get-CimInstance -ClassName Win32_BIOS
    Get-CimInstance -ClassName Win32_Share
    Get-CimInstance -ClassName Win32_OperatingSystem
    

    If you want to explore WMI, then the following code may come handy. Find-WmiClass accepts a simple keyword such as “video”, “network”, “ipaddress”. It then retrieves all WMI classes that contain the keyword…

    • 6 Feb 2018
  • Finding Nested AD Group Memberships

    The following code finds all groups a given Active Directory user is member of (including nested group memberships). The code requires the ActiveDirectory module.

    #requires -Module ActiveDirectory
     
    function Get-NestedGroupMember
    {
        param
        (
            [Parameter(Mandatory,ValueFromPipeline)]
            [string]
            $Identity
        )
     
        process
        {
            $user = Get-ADUser -Identity $Identity
            $userdn
    • 5 Feb 2018
  • Checking Network Connections

    If your machine is connected to the internet (or VPN) via different network connections, depending on where you are, then the following two functions may be useful to you.

    Get-ActiveConnection lists the names of all network connections that currently have an IP address assigned. Test-ActiveConnection accepts a keyword and checks to see whether there are currently any active connections with the keyword in their name.

    • 2 Feb 2018
  • Removing Bloatware from Windows 10

    Windows 10 comes with all kinds of preinstalled apps and other features that may feel annoying to some. Richard Newton has created a PowerShell script designed to identify and remove many preinstalled features, and strengthen privacy settings. He describes his project at https://github.com/Sycnex/Windows10Debloater.

    The source code can be found here: https://github.com/Sycnex/Windows10Debloater/blob/master/Windows10Debloater…

    • 1 Feb 2018
  • Managing FTP via PowerShell

    There are no built-in FTP commands in PowerShell, but you can easily download and install a free extension that provides the missing commands. Just run this line:

     
    PS> Install-Module -Name Posh-SSH -Scope CurrentUser
     

    If PowerShell cannot find the Install-Module command, then you are most likely not using the latest version of PowerShell (5.1). Please update your PowerShell installation, or add the Microsoft’s “PowerShellGet…

    • 31 Jan 2018
  • Formatting Numbers (Part 2)

    In the previous tip we introduced the Get-DisplayFileSize function which automatically converts bytes to readable numbers with units such as “KB” and “MB”.

    Using Select-Object, you can now produce folder listings with meaningful file sizes:

    $Length = @{
        Name = "Length"
        Expression = { 
        if ($_.PSIsContainer) { return }
        $Number = $_.Length
        $newNumber = $Number
        $u…
    • 30 Jan 2018
  • Formatting Numbers (Part 1)

    The following Get-DisplayFileSize function takes any byte value and returns a nicely formatted size, using units like “MB”, “GB”, or “PB”:

    function Get-DisplayFileSize 
    {
        param([Double]$Number)
     
        $newNumber = $Number
     
        $unit = ',KB,MB,GB,TB,PB,EB,ZB' -split ','
        $i = $null
        while ($newNumber -ge 1KB -and $i -lt $unit.Length)
        {
            $newNumbe…
    • 29 Jan 2018
  • Filtering Files

    You may not have noticed this, but the -Filter parameter on Get-ChildItem (aka dir or ls) is not as specific as you think. The following line should only find PowerShell scripts, but in fact it finds a lot more:

    Get-ChildItem -Path $env:windir -Filter *.ps1 -Recurse -ErrorAction Silent |
      Group-Object -Property Extension -NoElement 
    

    Here is the result:

     
    Count Name                     
    ----- ----                   …
    • 26 Jan 2018
  • Using List View in a Grid View Window (Part 3)

    In the previous tip we introduced ConvertObject-ToHashTable which makes it easy to dump objects in a grid view window.

    The code below is an improved version that sorts properties alphabetically, and lets you decide how the columns should be called. By default, Out-GridView calls the columns “Property” and “Value”, but you can override the defaults and call them anything:

    function ConvertObject…
    • 25 Jan 2018
  • Using List View in a Grid View Window (Part 2)

    Out-GridView is a useful cmdlet to list many objects. It is not ideal when you just want to display a single object with all of its properties, because in this scenario there would only be one line. In the previous tip we explained how turning an object to a hash table can help. It effectively puts the grid view in “ListView” mode.

    Because this approach can be highly useful in many scenarios, here is a function…

    • 24 Jan 2018
  • Using List View in a Grid View Window (Part 1)

    One of the simplest hardware inventories is probably this line:

    $data = systeminfo.exe /FO CSV | ConvertFrom-Csv
    $data | Out-GridView
    

    A more modern approach uses the new cmdlet Get-ComputerInfo:

    $data = Get-ComputerInfo
    $data | Out-GridView
    

    One simple approach is a hash table that is created for you by Group-Object: group the original data by a property like “UserName”. Then, show the hash table keys in the grid view…

    • 23 Jan 2018
  • Efficiently Produce Comma-Separated Strings (Part 2)

    In the previous tip we showed how you can use the PowerShell command mode to easily create lists of quoted strings. This can be really useful for creating code, and saves a lot of typing.

    Here is a function that can help in everyday PowerShell coding life:

    function s+ { "'$($args -join "','")'" | Set-ClipBoard }
    

    Next time you need a list of quoted strings in your code, simply type…

    • 22 Jan 2018
  • Efficiently Produce Comma-Separated Strings

    Here is a super simple approach to create a list of quoted strings:

    & { "'$($args -join "','")'" } hello this is a test
    

    Here is the result:

     
    'hello','this','is','a','test' 
     

    This approach effectively makes use of PowerShell’s “Command Mode” where literals are treated as arguments. You could even pipe this to Set-Clipboard and then paste…

    • 19 Jan 2018
  • Using a Grid View Window as a Selection Dialog (Part 2)

    In the previous tip we explained how you can use a hash table to show simple selection dialogs, yet when the user selects an item, return full rich objects.

    A hash table can basically use anything as a key. In the previous example, we used a string. It could as well be another object. This really leads you to a highly flexible approach for selection dialogs.

    Simply use Select-Object to select those properties that you…

    • 18 Jan 2018