Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



Three Ways to Execute a PowerShell Command

Three Ways to Execute a PowerShell Command

Here are basic instructions to help you execute a PowerShell command.  You have a choice of three strategies; firstly, the time-honoured method of copying other people's examples, and then pasting their code into your PowerShell session.  Secondly, creating cmdlets (my favorite), and thirdly, simply typing the instructions at the PowerShell command line.

Topics - How to Execute PowerShell Commands

 ♣

Method 1 Copying and Pasting (Easiest)

In PowerShell v 2.0 it's easier to use the ISE graphical version.

This is how to copy and paste PowerShell instructions to the command line version.

  • Launch Windows PowerShell
  • Copy all the lines of code into memory
  • Right-click on the PowerShell symbolPowerShell Scripts How to Copy and Paste
  • Edit --> Paste
  • Check the menus on my screenshot to the right
  • Press 'Enter' to execute your pasted code

The Process 'Vehicle' for Executing a PowerShell Command

Since the aim is to learn a technique, the practice code does not matter.  Most people use 'Hello World', as their test 'vehicle'; however, I prefer to choose a real example.  For instance, here is a cmdlet which gets the operating system processes, and then groups them by company name.

Example 1a: To List Processes Running on Your Machine

# PowerShell cmdlet to list running processes
Get-Process | Format-Table Name, company -auto

Example 1b: To Group Processes by Company

# PowerShell cmdlet to group processes by company
Clear-Host
Get-Process | Sort-Object company |ft -groupby company

Result: You should see a list of processes grouped by Company name.

Note: Ft is an alias for Format-Table.  Sort-Object has an alias of plain sort, which we use in the next script.

Example 1c: To Save List of Companies to File

# PowerShell cmdlet to group Processes by company
$Path = "C:\PowerShell\ProcessCompany.txt"
$ProSvc = Get-Process |sort company |ft -groupby company
$ProSvc
# $ProSvc | out-file $Path

Note: To save the results to a text file:
a) Check and amend the value of $Path. 
b) Remove the # from the beginning of the last line.

Next Step - Research More Built-in PowerShell Commands (Cmdlets)

Get-Command

# PowerShell Get-Command
Clear-Host
Get-Command -verb get

Note: PowerShell's Get-Command returns so many cmdlets that I have incorporated a filter so that it just lists cmdlets beginning with 'get'.  You could replace -verb get with the wildcard *.

Note: I suggest that you experiment next with Get-Process or Get-Eventlog

Guy Recommends: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, Solarwinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.  Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server.

Download your free copy of WMI Monitor

Method 2 Cmdlet (Best)

Because it saves your instructions permanently into a text file, this cmdlet method is an improvement over copy and paste.  Creating cmdlets is my favorite technique because it is ideal for making amendments, then re-running the commands.  As a bonus, cmdlets keep a record of what I have done.

  • Copy the code in the 'Vehicle' above into a text file
  • Save the file with a .ps1 extension, for example: addcontenta.ps1
  • In PowerShell v 2.0 you can save and open your .ps1 files in the ISE GUI
  • In PowerShell v 1.0, navigate to the folder where you saved addcontenta.ps1
    (D:\PowerShell\Files in the screen shot below)
  • Issue this command:
    .\addcontenta
    (dot backslash filename)

How to run a PowerShell cmdlet

Tip: For each of my PowerShell projects, I launch Windows Explorer and then create a subfolder.  Once I have a cmdlet that works, I store it in that subfolder.  Thereafter my technique is to call for: File (menu), SaveAs, create a new file.  Then I work with the new file and try to improve on that original version.  At various points I call for SaveAs again, thus creating a family of cmdlets, for example: addcontenta, addcontentz, addcontenty etc.

My reason for employing this cmdlet technique is twofold, firstly, to cater for that moment when my code gets into a tangle, and I think: 'If only I could go back in time to when my script WAS working'.  Well, thanks to saving lots of intermediary examples, I can revert to a working backup.  Secondly, producing cmdlets means that I can return to my research months later and pick up from where I left off, instead of starting the project from scratch.

You may like to combine methods 1 and 2 by copying other people's code then pasting, not to the command line, but into a cmdlet text file.  See more on creating cmdlets.

Guy Recommends:  Solarwinds' Free Bulk Import ToolFree Download of Solarwinds  Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD's attributes, click to import the users.  Optionally, you can provide the name of the OU where the new accounts will be born.

There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

Method 3 Type At the Command Line (Simplest Method)

Because PowerShell commands are so efficient, and thus short, I have no qualms about recommending that you simply type them at the command line (or in PowerShell 2.0, in the ISE GUI).  To test method 3, I have different examples or 'Vehicles'.  You could start by typing this at the command line: Get-Childitem c:\windows.

Indeed, if you are new to PowerShell there is nothing like typing to give you a 'feel' for the syntax.  As you type simple commands, so you get into the rhythm of the verb-noun pair.  Another bonus of typing is that you understand when to use a dash (-) and when to precede a dash with a space.  Here are three examples of what I mean:
Get-Eventlog -list  (Correct: Space before -list)
Get-Help Get-Eventlog (Correct: No space in the word 'eventlog')
Get-Eventlog system | Get-Member  (Correct: No space between each verb-noun)

get  -eventlog -list  (Wrong: Space between get and -)
Get-help -eventlog (Wrong: 'Overthink' eventlog is not a parameter or a switch, it is a positional argument and thus does not need a dash.

There are occasions when even experienced PowerShell scriptwriters resort to typing commands.  As for me, I love creating cmdlets, but I do type commands especially when I want a list of an object's properties, for example,
abc-xyz |gm.  (gm is an alias for Get-Member).

Summary of How to Execute a PowerShell Command

Typical Microsoft, there are always at least two ways of executing PowerShell code.  By all means start with the time-honoured method of copying and pasting, but for the long term, my advice is take the time to master the cmdlet method.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tutorials

PowerShell Tutorials  • Methods  • Cmdlets  • PS Snapin  • Profile.ps1  • Exchange 2007

Command & Expression Mode  • PowerShell pipeline (|)  • PowerShell 'where'  • PowerShell 'Sort'

If you see an error of any kind, do let me know.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

 *


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.