Skip to content

Instantly share code, notes, and snippets.

@kirill-d-lappo
Created February 16, 2021 10:24
Show Gist options
  • Select an option

  • Save kirill-d-lappo/369901673d7c43b5150c51c0fa68302b to your computer and use it in GitHub Desktop.

Select an option

Save kirill-d-lappo/369901673d7c43b5150c51c0fa68302b to your computer and use it in GitHub Desktop.
Powershell 7 : Parallel Functions For Windows Service Manipulations
#requires -version 7
# Requires pwsh 7 or higher
# Add functions to your $PROFILE so they are always accessible
function Restart-ServiceParallel{
[CmdletBinding()]
param (
[Parameter()]
[string]
$Name
)
Get-Service $Name | ForEach-Object -Parallel { Restart-Service $_ }
}
function Start-ServiceParallel{
[CmdletBinding()]
param (
[Parameter()]
[string]
$Name
)
Get-Service $Name | ForEach-Object -Parallel { Start-Service $_ }
}
function Stop-ServiceParallel{
[CmdletBinding()]
param (
[Parameter()]
[string]
$Name
)
Get-Service $Name | ForEach-Object -Parallel { Stop-Service $_ }
}
function Remove-ServiceParallel{
[CmdletBinding()]
param (
[Parameter()]
[string]
$Name,
[Parameter()]
[switch]
$Force
)
if (!$Force){
$response = Read-Host -Prompt "Removed services can't be restored! Are you sure? (yes/no)"
if ($response -eq 'y'){
$response = Read-Host -Prompt "Type 'yes'"
}
if ($response -ne 'yes'){
Write-Host "Aborted service removal."
return
}
}
Get-Service $Name | ForEach-Object -Parallel { Remove-Service $_ }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment