
powershell 命令
Start-Process
is a powershell commandlet used create new process. As powershell is mainly developed .Net and provides .Net feature Start-Process
is equivalent of System.Diagnostics.Process
method.
Start-Process
是一个Powershell Commandlet,用于创建新进程。 由于powershell主要是.Net的开发并提供.Net功能,因此Start-Process
等效于System.Diagnostics.Process
方法。
开始一个过程 (Start A Process)
The most basic usage of the Start-Process
is start a process by giving its name. Start-Process inherits current working environment variables and can find given executable if its in Path
environment. In this example we will start notepad.exe
by specifying a filename names.txt
Start-Process
最基本用法是通过给出其名称来启动一个过程。 启动过程会继承当前的工作环境变量,并且可以在Path
环境中找到给定的可执行文件。 在此示例中,我们将通过指定文件名names.txt
来启动notepad.exe
。
PS> Start-Process notepad.exe names.txt
运行批处理文件 (Run Batch File)
We can specify a script file or batch file to run in Powershell. We will use -filepath
option with the full path and name of batch file. In this example we run batch file backup.cmd
.
我们可以指定脚本文件或批处理文件以在Powershell中运行。 我们将使用-filepath
选项以及批处理文件的完整路径和名称。 在此示例中,我们运行批处理文件backup.cmd
。
PS> Start-Process -filepath C:\backup.cmd
以提升的管理员权限运行 (Run With Elevated Admin Privileges)
While running and creating new processes with Start-Process the current user privilege will be get. This may not work some situations. We can elevate privileges to the Administrator with -verb runas
. In this example we will run cmd.exe
with Administrator privileges.
在使用Start-Process运行和创建新流程时,将获得当前用户权限。 在某些情况下这可能不起作用。 我们可以使用-verb runas
将特权提升给管理员。 在此示例中,我们将以管理员权限运行cmd.exe
。
PS> Start-Process -verb runas cmd.exe
设置工作目录 (Set Working Directory)
While working with Start-Process
by default current working directory will be current working directory. Setting working directory of given process can be changed with -workingdirectory
option. This is useful if we need to change executable file path. In this example we will change to the C:\Windows
to the working directory. Providing directories in double quotes will make this less error prone.
默认情况下,使用“ Start-Process
,当前工作目录将是当前工作目录。 可以使用-workingdirectory
选项更改给定进程的设置工作目录。 如果我们需要更改可执行文件路径,这将很有用。 在此示例中,我们将C:\Windows
更改为工作目录。 用双引号提供目录将减少这种错误。
PS> Start-Process -workingdirectory "C:\Windows" cmd.exe
使用打印动词 (Use Print Verb)
We can also use Start-Process
without providing any executable file. We can use print verb to print text files. We will use -verb Print
for this. In this example we will print the file named names.txt
我们也可以使用Start-Process
而不提供任何可执行文件。 我们可以使用打印动词来打印文本文件。 我们将为此使用-verb Print
。 在此示例中,我们将打印名为names.txt
的文件
PS> Start-Process names.txt -verb Print
设置WindowStyle (Set WindowStyle)
While starting the new process it will open new windows in default size. We can change this window size on startup. We can use -windowstyle
option. This option can get following values
在开始新过程时,它将以默认大小打开新窗口。 我们可以在启动时更改此窗口大小。 我们可以使用-windowstyle
选项。 此选项可以获取以下值
- Maximized最大化
- Minimized最小化
In this example we will open notepad.exe
in maximized window.
在此示例中,我们将在最大化窗口中打开notepad.exe
。
PS> Start-Process notepad.exe -windowstyle Maximized
重定向标准输入 (Redirect Standard Input)
Commands and executables running in a shell will have some standard input. Standard input provided data will be fed into command or executable. We can use -RedirectStandardInput
option with the file which contains data to be fed into executable. In this example we will put data in mydata.txt
into notepad.exe
.
在Shell中运行的命令和可执行文件将具有一些标准输入。 标准输入提供的数据将被馈送到命令或可执行文件中。 我们可以将-RedirectStandardInput
选项与包含要馈入可执行文件的数据的文件一起使用。 在此示例中,我们将mydata.txt
数据放入notepad.exe
。
PS> Start-Process notepad.exe -RedirectStandardInput mydata.txt
重定向标准输入 (Redirect Standard Input)
As done in previous example we can redirect standard output into given file. We will use -RedirectStandardOuput
with a file name. In this example we will redirect into file named notepad.log
.
就像前面的例子一样,我们可以将标准输出重定向到给定的文件中。 我们将使用-RedirectStandardOuput
和文件名。 在此示例中,我们将重定向到名为notepad.log
文件。
PS> Start-Process notepad.exe -RedirectStandardOutput notepad.log
翻译自: https://www.poftut.com/powershell-start-process-command-tutorial-examples/
powershell 命令