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. |
Invoke-Item c:\scripts\*.txt
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. |
c:\scripts\test.txt
Invoke-Item Aliases |
---|
|
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
& c:\scripts\test.ps1
c:\scripts\test.ps1
Invoke-Expression "c:\my scripts\test script.ps1"
Invoke-Expression Aliases |
---|
|
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}
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
Measure-Command {c:\scripts\test.ps1}
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
No comments:
Post a Comment