powershell 命令_具有示例的Powershell启动过程命令教程

powershell 命令

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

    最小化
LEARN MORE  What Is Multithreading In Operating Systems and Programming?
了解更多信息什么是操作系统和编程中的多线程?

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 命令

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值