An example of calling external and process operations commands in PowerShell _powershell

Source: Internet
Author: User

Learning PowerShell, we don't expect to do everything through C #, and we should remember that Cmd.exe or batch processing is a valuable asset to us-by invoking external programs to solve problems. Call the external program, is bound to the process of management, this article is to introduce.

1, Get-process, return to the process.

get-process [-id], get by PID
Get-process-name, by Process name
Get-process-inputobject, passing parameters
For example: Get-process-name MMC, which shows the process for all MMC.

2, Start-process, start a process

Syntax: Start-process-filepath < executable path >-argumentlist < parameter list >
Description: In PowerShell v2.0 above only support!

3, stop-process, stop one or more running processes.
stop-process [-id], stopping the process of the specified PID array
Stop-process-name, stop a list of process names
Stop-process-inputobject, stops all processes in an array of process-type processes.

such as: Stop-process-name Notepad, this function can also be achieved:
$pro = Get-process-name Notepad; $pro. Kill (); or (Get-process-name notepad). Kill ();

Windows provides a Taskkill.exe program through which you can also stop programs: taskkill/f/im Java.exe

With this command, when we want to end the Java.exe running in Cmd.exe, we only end the Java.exe, this time CMD will also be ended.

4, use Point (.) Call PS1 or batch file (invoke-expression)
syntax:.
Note: You can invoke an existing PS1 file in the PowerShell interaction environment and PS1 file, using dots (.) as the caller. I've tried using Invoke-item, as if I didn't start it.
You can also use the invoke-expression (alias-IEX) command for two uses:

Copy Code code as follows:

C:\ps>invoke-expression-command "C:\ps-test\testscript.ps1"
C:\ps> "C:\ps-test\testscript.ps1" | Invoke-expression

Note: You can use PS1 here, or you can use other executable batch files, such as. cmd,. bat, and so on.

5, open the file by default open mode (Invoke-item)
Syntax: Invoke-item < file path to open >
For example: Invoke-item "C:\1.txt" #将使用txt文件的默认打开工具 (that is, notepad) Open C:\1.txt
Note: The file must exist, otherwise the error "This path does not exist" is reported.

6. Using & Calling procedure
Syntax:& < executable path > [< parameter list
Example:

Copy Code code as follows:
$execPath = "D:\progra~1\flashfxp\flashfxp.exe"
$execArgs = "-upload ftp://u:p@ip:21"
$execArgs = $execArgs + "-remotepath= '"/"" "
$execArgs = $execArgs + "-localpath= '" d:\123\ ' ""
& $execPath $execArgs. Split ()

7. Use CMD to execute DOS internal or external command
Description: There are many DOS commands that are occupied by the alias of the cmdlet, so if you use it as before, you may find that the parameters are incorrect. In order to be compatible with previous batch usage, you can use CMD/C to invoke DOS internal or external commands.
Syntax: cmd/c
Example: cmd/c del/s/q d:\testdir\testsubdir\*.*

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.