Three Ways to Execute a PowerShell CommandThree Ways to Execute a PowerShell CommandHere 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 a PowerShell Command
♣ Method 1 Copying and Pasting (Easiest)This is how to copy and paste PowerShell instructions at the command line.
'Vehicle' for Executing a PowerShell CommandSince 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. Incidentally, the code is a work-in-progress where I am trying to output the data to a file called ProcessCompany.txt. Example 1: To list Processes running on your machine # PowerShell cmdlet to group Processes by company Result: You should see a list of processes grouped by Company name. 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.
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 to twofold, 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. 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. 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
(Wrong: Space between get and -) 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,
Guy Recommends: SolarWinds LANSurveyor
|
|||||
|
|
|
|
Home Copyright © 1999-2009 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |