New-Service
创建新的 Windows 服务。
语法
New-Service
[-Name] <String>
[-BinaryPathName] <String>
[-DisplayName <String>]
[-Description <String>]
[-StartupType <ServiceStartMode>]
[-Credential <PSCredential>]
[-DependsOn <String[]>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
说明
New-Service cmdlet 为注册表和服务数据库中的 Windows 服务创建新条目。 新服务需要一个在服务期间运行的可执行文件。
使用此 cmdlet 的参数可以设置服务的显示名称、说明、启动类型和依赖项。
示例
示例 1:创建服务
PS C:\> New-Service -Name "TestService" -BinaryPathName "C:\WINDOWS\System32\svchost.exe -k netsvcs"
此命令创建名为 TestService 的服务。
示例 2:创建包含说明、启动类型和显示名称的服务
PS C:\> New-Service -Name "TestService" -BinaryPathName "C:\WINDOWS\System32\svchost.exe -k netsvcs" -DependsOn NetLogon -DisplayName "Test Service" -StartupType Manual -Description "This is a test service."
此命令创建名为 TestService 的服务。 它使用 New-Service 的参数来指定新服务的说明、启动类型和显示名称。
示例 3:查看新服务
PS C:\> Get-WmiObject win32_service -Filter "name='testservice'"
ExitCode : 0
Name : testservice
ProcessId : 0
StartMode : Auto
State : Stopped
Status : OK
此命令使用 Get-WmiObject 获取新服务的 Win32_Service 对象。 此对象包括启动模式和服务说明。
示例 4:删除服务
PS C:\> sc.exe delete TestService
- or -
PS C:\> (Get-WmiObject win32_service -Filter "name='TestService'").delete()
此示例演示了删除 TestService 服务的两种方法。 第一个命令使用 Sc.exe的 delete 选项。 第二个命令使用 Delete 方法 Get-WmiObject 返回的Win32_Service对象。
参数
-BinaryPathName
指定服务的可执行文件的路径。 此参数是必需的。
类型: | String |
Position: | 1 |
默认值: | None |
必需: | True |
接受管道输入: | False |
接受通配符: | False |
-Confirm
在运行 cmdlet 之前,提示你进行确认。
类型: | SwitchParameter |
别名: | cf |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-Credential
将服务使用的帐户指定为 服务登录帐户。
键入用户名(如 User01 或 Domain01\User01),或输入 PSCredential 对象,例如由 Get-Credential cmdlet 生成的用户名。 如果键入用户名,此 cmdlet 会提示输入密码。
类型: | PSCredential |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-DependsOn
指定新服务所依赖的其他服务的名称。 若要输入多个服务名称,请使用逗号分隔名称。
类型: | String[] |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-Description
指定服务的说明。
类型: | String |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-DisplayName
指定服务的显示名称。
类型: | String |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-Name
指定服务的名称。 此参数是必需的。
类型: | String |
别名: | ServiceName |
Position: | 0 |
默认值: | None |
必需: | True |
接受管道输入: | False |
接受通配符: | False |
-StartupType
设置服务的启动类型。 此参数的可接受值为:
- 手动。 服务仅由用户使用服务控制管理器或应用程序手动启动。
- 自动。 在系统启动时,服务已启动或由操作系统启动。 如果自动启动的服务依赖于手动启动的服务,则系统启动时也会自动启动手动启动服务。
- 禁用。 服务已禁用,不能由用户或应用程序启动。
默认值为“自动”。
类型: | ServiceStartMode |
接受的值: | Automatic, Manual, Disabled |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-WhatIf
显示 cmdlet 运行时会发生什么情况。 cmdlet 未运行。
类型: | SwitchParameter |
别名: | wi |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
输入
None
不能通过管道将输入传递给此 cmdlet。
输出
此 cmdlet 返回一个表示新服务的对象。
备注
- 若要在 Windows Vista 和更高版本的 Windows 操作系统上运行此 cmdlet,请使用“以管理员身份运行”选项启动 Windows PowerShell。
- 若要删除服务、使用 Sc.exe或使用 Get-WmiObject cmdlet 获取表示服务的 Win32_Service 对象,然后使用 Delete 方法删除服务。 Get-Service 返回的对象没有 delete 方法。