Wednesday, January 31, 2018

Use Tee-Object to Split Output

When obtaining data using PowerShell, sometimes we want it displayed to the screen. Sometimes we want it saved to a variable.

But if we use Tee-Object, we can do both:

Get-Process | Select-Object Name -First 1 | `
   Tee-Object -Variable FirstProcess




 
In this command, we're getting all the processes running on the computer. Then we pipe it to the Select-Object command to pick out the first one alphabetically. Then we pipe it to the Tee-Object, which both prints it to the screen and populates the variable $FirstProcess with the value.

Tuesday, January 30, 2018

Find Service Dependencies

We can use PowerShell to find services that have the same dependencies. Just use the -RequiredServices switch and provide the service name:


Get-Service -Required Services rpcss




This would be useful if, say, we can't get a particular service to start. Are there any other services that need to be started first?











Monday, January 29, 2018

Create Computer Restore Point

Our Windows computer should be creating a new Restore Point every time it reboots. But creating one for ourselves is a good idea, particularly when we're about to make a major change. 

That's easy to do in PowerShell when launched with administrator rights using the Checkpoint-Computer cmdlet:

Checkpoint-Computer -Description "Pre-DotNetUpgrade"

 


Once completed, we can use Get-ComputerRestorePoint to verify:

 

No, I don't know why Microsoft refers to it as both a 'Checkpoint' and a 'RestorePoint'. 

Friday, January 26, 2018

Find Restore Points

We can use PowerShell to quickly display our computer's Restore Points:

Get-ComputerRestorePoint


Wednesday, January 24, 2018

Update PowerShell's Help Files


PowerShell comes with built-in help files to explain how its commands work. But tweaks and updates occur all the time.


The way to keep your help files up-to-date is through a cmdlet called Update-Help:








We'll see the progress indicator as the files install.

Some things to remember:
  • Running this command requires PowerShell be launched as an Administrator.
  • By default, Update-Help updates the help files for all the modules (packages of commands) that are loaded, and for most other modules as well, as long as they are updateable. 
  • If we wanted to update the help files for a specific module, we can do so with the -Module parameter:
Update-Help -Module <module name>
 





  • By default, Update-Help will only run once per day per computer. To override the once-per-day limit, use the Force parameter.






    Tuesday, January 23, 2018

    PowerShell Version Check

    Sometimes, certain PowerShell scripts or commands require a particular version of PowerShell to be installed. A command that works just fine with, say, version 3 will throw an error if using version 2.

    One way to prevent the error is to first check which version is available. We can develop logic to conduct one action or another, depending on the version:

       $PSVersion = $PSVersionTable.PSVersion.Major 

    Here we created a variable called $PSVersion and made it equal to the Major PowerShell version, using dot notation to get just the relevant number:

     

    Now we can use this variable in an if...else statement to decide on a course of action:

        if ($PSVersion -eq 4) {
            Write-Output "Your PowerShell version is $PSVersion. `

            The script should work fine."
        } else {
            Write-Output "Your PowerShell needs to be updated for `

            this script to work properly."
        }



    First we used the if command to check to see if the variable $PSVersion is equal to (or '-eq' for short) the number '4'. If it is, then the Write-Output command within the first pair of braces is executed. If the variable is equal to anything else, then the command with the second pair of braces is executed. 

    This would be a useful set of commands to add to the beginning of a script to inform the user if their version of PowerShell will be suitable.
     

    Find Your PowerShell Version

    To find what version of PowerShell we have installed, enter this variable:

    $PSVersionTable



     

    This lists other components necessary for PowerShell, but it may be more information that we really need. To find just the version of PowerShell, we can use dot notation to tease out the relevant figure:

    $PSVersionTable.PSVersion
    $PSVersionTAble.PSVersion.MajorVersion



    And if necessary, we can pipe this information to the clipboard to paste into other applications or documents:

    $PSVersionTable | clip.exe