Thursday, February 17, 2011

What can I do with windows powershell part10

Scripts and Applications


The following articles provide a brief introduction to working with scripts and applications while using Windows PowerShell; these tasks include such things as running scripts and/or applications as well as measuring how long it takes a Windows PowerShell task to complete. As is so often the case with Windows PowerShell, the code snippets found in these articles can either be included within a Windows PowerShell script or typed directly into the Windows PowerShell console.

Using the Invoke-Item Cmdlet

Opening a File or Running a Program

The Invoke-Item cmdlet provides a way to run an executable file or to open a file (or set of files) from within Windows PowerShell. For example, this command opens Calculator:
Invoke-Item c:\windows\system32\calc.exe
Note. Yes, by default you must specify the entire path to the executable file.
This command, meanwhile, opens all the .txt files found in C:\Scripts:
Invoke-Item c:\scripts\*.txt
To tell you the truth, the latter example - using Invoke-Item to open multiple items at once - is probably the best use of this cmdlet. If all you want to do is open a single document or launch a single program, that can be done just by typing the document/application name and pressing ENTER. Want to start Notepad? This command will do that for you:
notepad
Note. Interestingly enough, you don’t need to specify the entire file path if you start a program without actually calling Invoke-Item. Of course, that’s true only if the application is in your Windows path.
Want to open the file C:\Scripts\Test.txt in Notepad (or whatever program you use as the default editor for .txt files)? Then just type in the path to the file:
c:\scripts\test.txt
Invoke-Item Aliases
  • ii
Using the Invoke-Expression Cmdlet

Running a Windows PowerShell Script

The Invoke-Expression cmdlet provides one way to run a script from within Windows PowerShell. For example, this command runs the script C:\Scripts\Test.ps1:
Invoke-Expression c:\scripts\test.ps1
Alternatively you can use the ampersand to indicate that you want to run a script:
& c:\scripts\test.ps1
Or, even easier, just type in the path to the script:
c:\scripts\test.ps1
In all cases any paths that include blank spaces must have the entire path enclosed in double quote marks:
Invoke-Expression "c:\my scripts\test script.ps1"
Invoke-Expression Aliases
  • iex
Using the Measure-Command Cmdlet

Timing How Long it Takes a Script or Command to Run

For better or worse people are often concerned with the running time of their scripts and their commands. If you’ve ever wondered how long it takes to perform a specific task then the Measure-Command cmdlet is exactly what you’ve been looking for: it enables you to measure the running time of a command or script down to the millisecond. (Actually, it even enables you to measure the running time beyond a millisecond. But at that point ….)
To use Measure-Command, simply call the cmdlet, using the command or script to be run as the cmdlet parameter (make sure you enclose the value within curly braces). For example, this command runs the Get-Service cmdlet and then exports the data to an XML file named C:\Scripts\Test.xml; at the same time, it also keeps track of the time it takes for the command to complete:
Measure-Command {Get-Service | Export-Clixml c:\scripts\test.xml}
When you run this command, service information will be saved to the file Test.xml; in addition, Measure-Command will report back information similar to this:
Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 980
Ticks             : 9807034
TotalDays         : 1.13507337962963E-05
TotalHours        : 0.000272417611111111
TotalMinutes      : 0.0163450566666667
TotalSeconds      : 0.9807034
TotalMilliseconds : 980.7034
To perform this same feat with a script simply pass the name of that script as the Measure-Command parameter. This command runs the script C:\Scripts\Test.ps1, and - as a bonus - reports back the running time as well:
Measure-Command {c:\scripts\test.ps1}
Here’s what we get back:
Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 971
Ticks             : 9717422
TotalDays         : 1.12470162037037E-05
TotalHours        : 0.000269928388888889
TotalMinutes      : 0.0161957033333333
TotalSeconds      : 0.9717422
TotalMilliseconds : 971.7422
As you can see, at least in this case there’s no practical difference between running the command from the Windows PowerShell console and running that very same command from a Windows PowerShell script.

No comments:

Post a Comment