5 Basic PowerShell Commands You Should know!

If you haven’t caught the PowerShell bug yet, NOW IS THE TIME! Here are 5 Basic PowerShell commands to get you started.

Get-help

Like the man command in UNIX, Get-Help in PowerShell contains helpful information about every cmdlet in PowerShell. Not only does the help feature contain information, but it also contains examples on how to use the cmdlet.

For example:
Get-Help Get-Process” would gather basic information about the “Get-Process” cmdlet. You can also just enter the following “Help Get- Process”, or even “help gps”. GPS is the Alias for Get-Process.  If you want even more information about a command, you can use the “-Full” switch. This will return everything the Help page has on the cmdlet.

PS C:\>Get-Help Get-Process -Full

I learn best by example, so the “-Example” switch is my favorite. Entering “Get-Help Get-Process -Example”, will display examples on how to use the cmdlet, along with switches, formatting, and different options that you can use.

Set-ExecutionPolicy

Microsoft decided to setup security in PowerShell with the “Set-ExecutionPolicy” cmdlet. Unlike VBScript where you can execute a command without any restrictions, PowerShell has disabled scripting by default in an effort to prevent malicious code from executing on your system. You can use the “Set-ExecutionPolicy” cmdlet to set the execution policy on your server. (Note: This only needs to be done once).

There are five levels of execution.

  1. Restricted: Does not load configuration files or run scripts. “Restricted” is the default.
  2. AllSigned: Requires that all scripts and configuration files be signed by a trusted publisher, including scripts that you write on the local computer.
  3. RemoteSigned: Requires that all scripts and configuration files downloaded from the Internet be signed by a trusted publisher.
  4. Unrestricted: Loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the Internet, you are prompted for permission before it runs.
  5. Bypass: Nothing is blocked and there are no warnings or prompts.

There is also a “undefined” setting that removes the currently assigned execution policy from the current scope. This parameter will not remove an execution policy that is set in a Group Policy scope.

To set your execution policy, all you need to do is type “Set-ExecutionPolicy” and whatever value you want to set.

PS C:\>Set-ExecutionPolicy RemoteSigned

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic. Do you want to change the execution policy?
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y")

After you run that cmdlet, you will be prompted with the following. Type “Y” to continue. You can also use the “-Force” switch to skip the “are you sure” box.

The “Get-ExecutionPolicy” does what you think. It “gets” the current ExecutionPolicy of the system. Just type “Get-ExecutionPolicy”, in your PowerShell window.

PS C:\>Get-ExecutionPolicy

Import-Module

A module is a package that contains members (such as cmdlets, providers, scripts, functions, variables, and other tools and files) that can be used in Windows PowerShell. Has PowerShell continues to catch on, more and more companies are creating modules. Exchange, Active Directory, WSUS, Lync, SharePoint, and even VMware have modules. To view which modules are already on your server type the following command.

PS C:\> Get-Module –ListAvailable

To view which modules are already loaded in Powershell you can use the following.

PS C:\> Get-Module

To add a new module to your PowerShell enviroment, just type “Import-Module“, and then the Modules name.

 Import-Module TroubleshootingPack 

Get-Content

As a SysAdmin, you will be looking at a ton of log, and configuration files. Because of this, the “Get-Content” cmdlet should be well known to you. When using “Get-Content“, you can rapidly read and display the contents of any text files on your system. You can also create a text file with a list of servers in your environment, and then use the “Get-Content” command to pull the list into a Function or script you wrote. Using the “Get-Content” cmdlet is simple.

PS C:\> Get-Content "C:\textfile.txt"

The command above will display every line of text in that file. Let’s say however you only want the last five lines of the text file. Easy.

PS C:\> Get-Content "C:\textfile.txt" | Select-Object -last 5 

You can also use “Select-Object -first 5” 4, 3, or any number on lines you want.

The cool thing about “Get-Content“, is you can also select strings out of the content, or even match content. If you are good at Regular Expression, you can even use that to find what you are looking for.

Start-Transcript

Since we are responsible Admin’s we want to make sure everything we do on the server is documented. The “Start-Transcript” cmdlet is perfect for that. This cmdlet creates a record of every command you run in Windows PowerShell , and writes it to a text file. This can come in handy for audit purposes, or in my case, what was that command I ran yesterday? Run “Start-Transcript” when you first open up your PowerShell window, and you are all set. You can also change the output of the text file by typing the following.

C:\PS>start-transcript -path c:\someplace\transcript.txt

You can also add the “-noclobber” switch to the end of the line to prevent the file from being overwritten.

What I like to do is add “Start-Transcript” to my PowerShell profile. That way I don’t have to remember to start the transcript every time I open PowerShell. I also get a little fancy with it, by changing the log path, renaming the log file to today’s date. In doing this, I can have a PowerShell log for each day of the year.

##--------------------------------------------------------------------------
## Begin Logging
##--------------------------------------------------------------------------
$logPath = "C:\WindowsPowerShell\logs\"
$logFileName = "$(get-date -f yyyy-MM-dd).txt"
$logFile = $logPath + $logFileName
Start-Transcript -path $logFile -append

 

Leave a Reply

avatar

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

  Subscribe  
Notify of