Helpful Powershell Commands to Know

Microsoft PowerShell Recently I was talking with a colleague about our histories of working in the computer field, all the ways from the early days of DOS to where we were currently.  I remember the early days of working on the computers and using the DOS commands to get stuff completed, today Microsoft has started using PowerShell for the admin commands of todays’ systems.  So with the push in PowerShell and me using it more and more, I figured I’d go over the commands that every admin should know.

By no means is this every command in PowerShell that you should be familiar with, but it is a good start.  If you work with PowerShell that most likely you’ve already used these commands and are quite familiar with them.

 

  1. Get-Help
    Just like with most programs, one of the first key’s for learning is by using the built in help file (or by hours of trial and error).  This Get-Help cmdlet can be used with other commands to for information on a particular command. For example, to find out all the commands you can use with the Get verb, type:

    Get-Help -Name Get-*
  2. Set-ExecutionPolicyJust like with any kind of command line product, you may want to create some PowerShell scripts, and you may wonder why at first it isn’t working.  Microsoft has disabled the ability for PowerShell scripts by default in order to prevent someone from executing some bad code.  You can use this cmdlet to set the security level for executing the PowerShell scripts at four different security levels:

    Restricted
    — Restricted is the default execution policy and locks PowerShell down so that commands can be entered only interactively. PowerShell scripts are not allowed to run.

    All Signed
    — If the execution policy is set to All Signed then scripts will be allowed to run, but only if they are signed by a trusted publisher.

    Remote Signed
    — If the execution policy is set to Remote Signed, any PowerShell scripts that have been locally created will be allowed to run. Scripts created remotely are allowed to run only if they are signed by a trusted publisher.

    Unrestricted
    — As the name implies, Unrestricted removes all restrictions from the execution policy. 

    You can set an execution policy by entering the Set-ExecutionPolicy command followed by the name of the policy. For example, if you wanted to allow scripts to run in an unrestricted manner you would type:
    Set-ExecutionPolicy Unrestricted

     

  3. Now let’s say you are working on a server you’ve never seen before and.or are unaware of the execution policy, you can run Get-Execution-Policy command.

  4. Get-Service
    The Get-Service command provides a list of all of the services that are installed on the system.  If you are interested in a specific service you can append the -Name switch and the name of the service (wildcards are permitted) When you do, Windows will show you the service’s state. 

     

  5. ConvertTo-HTML/Export-CSVIf like me sometimes looking at information on the screen isn’t enough or you may want to document something or be able to email a report to someone, that helpful way is the ConvertTo-HTML command or Export-CSV.  To use this command, simply pipe the output from another command into the ConvertTo-HTML command. You will have to use the -Property switch to control which output properties are included in the HTML file and you will have to provide a filename.  The same thing goes with the CSV file.Get-Service | ConvertTo-HTML -Property Name, Status > C:\services.htm 

     

  6. Get-Process/Stop-ProcessJust as we can use the Get-Service command to display a list of all of the system services, you can use the Get-Process command to display a list of all of the processes that are currently running.  Sometimes, a process will freeze up, and when this happens, you can use the Get-Process command to get the name or the process ID for the process that has stopped responding. You can then stop the process by using the Stop-Process command. A neat thing is that you can terminate a process based on its name or its process ID.

    Stop-Process -Name notepad
    Stop-Process -ID 2457

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.