Essential skill you should know: Mapping PowerShell commands to variables

When I first started learning about PowerShell many years ago, one of the concepts that I had trouble wrapping my head around was that of mapping PowerShell commands to variables. In case you have never encountered this before, PowerShell makes it possible to link a variable to a command so that when you call the variable, the command is executed. While there is nothing overly difficult about mapping PowerShell commands to a variable, it can sometimes be tricky to get the variable to give you the output that you might expect. As such, I thought that I would share with you a few tricks that I have picked up over the years.

Read More

  • But...But....But... this is not mapping PS commands to a variable. It's assigning the output results / object to a variable and then using the variable name captured property data to get the property contents / values.

    This is mapping a PS command to a variable, though I am not real sure why one would do it.

        # Store the command
        $MyVar = "Get-PhysicalDisk"

        # Using the command
        Invoke-Expression -Command $MyVar

    As for this statement...
    "In actuality, this block of code would show me the health status of every SSD in the system. In this case, though, I only have one. If I had multiple SSDs and needed to narrow down the list, I could filter by the disk’s friendly name or serial number."

    These lists are just zero-based arrays. So one could just pick a drive by it's position in the array.

    4 SSD / HHD would mean array list 0-3.

        ($SSD = Get-PhysicalDisk | Where MediaType -eq ‘SSD’)

        # Results
        FriendlyName              SerialNumber         MediaType CanPool OperationalStatus HealthStatus Usage            Size
        ------------              ------------         --------- ------- ----------------- ------------ -----            ----
        Samsung SSD 850 EVO 2TB   S2HCNWAG901905D      SSD       False   OK                Healthy      Auto-Select   1.82 TB
        Samsung SSD 950 PRO 512GB 0025_3853_5B16_0295. SSD       False   OK                Healthy      Auto-Select 476.94 GB
        Samsung SSD 850 EVO 4TB   S2RSNX0J300191B      SSD       False   OK                Healthy      Auto-Select   3.64 TB
        Samsung SSD 950 PRO 512GB 0025_3853_5B16_000D. SSD       False   OK                Healthy      Auto-Select 476.94 GB

    So, 4 SSD's in my system, all with the same default friendlyName, as most folks won't rename them. So, rather than mess with friendlyName or serial number, if I wan the first drive, it's just...

        $SSD[0]

        FriendlyName            SerialNumber    MediaType CanPool OperationalStatus HealthStatus Usage          Size
        ------------            ------------    --------- ------- ----------------- ------------ -----          ----
        Samsung SSD 850 EVO 2TB S2HCNWAG901905D SSD       False   OK                Healthy      Auto-Select 1.82 TB