@GitHub Copilot - Welcome @BanterBoy, I’m your Copilot and I’m here to help you get things done faster. I can identify issues, explain and even improve code.

You can ask generic questions, but what I’m really good at is helping you with your code. For example:

Generate unit tests for my code Explain the selected code Propose a fix for the bugs in my code If you want to learn more about my capabilities and limitations, check out the Copilot documentation.

I’m powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions, and share feedback so that we can learn and improve.

@BanterBoy: - Explain what the script does and why it is useful.

@GitHub Copilot - The code is a PowerShell script that defines a function called Restart-ExchangeServices. This function takes two parameters: $ComputerName and $Credential. $ComputerName is an array of strings that represents the names of the computers on which the Exchange services will be restarted. $Credential is an optional parameter that represents the credentials that will be used to connect to the remote computers.

The function uses the CmdletBinding attribute to specify some metadata about the function. This attribute allows the function to support common parameters like -Verbose, -Debug, and -ErrorAction. It also specifies a default parameter set, a confirmation level, and a help URI.

The function uses a foreach loop to iterate over each computer name in the $ComputerName array. For each computer, it checks if the current operation should be confirmed by the user by using the ShouldProcess method. If the user confirms the operation, the function creates a new PowerShell session on the remote computer using the New-PSSession cmdlet. It then defines a script block that gets all the Exchange-related services that are currently running on the remote computer and restarts them using the Restart-Service cmdlet.

Finally, the function uses the Invoke-Command cmdlet to run the script block on the remote computer using the PowerShell session that was created earlier. The output of the function is a string that represents the name of the computer on which the Exchange services were restarted.

Possible ways to improve the code with respect to readability and performance include adding comments to explain the purpose of each section of the code, using more descriptive variable names, and using the -Filter parameter of the Get-Service cmdlet to filter the services more efficiently. Additionally, the function could be modified to support other parameters like -ServiceName to specify a specific service to restart, or -Force to force the restart of all services without prompting the user.

@BanterBoy: - Explain how to use the script and provide examples.

@GitHub Copilot - Certainly! Here are a few examples of how you could use the Restart-ExchangeServices function:

  1. Restart the Exchange services on a single remote computer:
Restart-ExchangeServices -ComputerName "SERVER01"
  1. Restart the Exchange services on multiple remote computers:
Restart-ExchangeServices -ComputerName "SERVER01", "SERVER02", "SERVER03"
  1. Restart the Exchange services on multiple remote computers using different credentials:
$Credential = Get-Credential
Restart-ExchangeServices -ComputerName "SERVER01", "SERVER02", "SERVER03" -Credential $Credential
  1. Restart the Exchange services on multiple remote computers without prompting for confirmation:
Restart-ExchangeServices -ComputerName "SERVER01", "SERVER02", "SERVER03" -Confirm:$false
  1. Restart a specific Exchange-related service on a remote computer:
Restart-ExchangeServices -ComputerName "SERVER01" -ServiceName "MSExchangeTransport"

These are just a few examples of how you could use the Restart-ExchangeServices function. You can customize the function to suit your specific needs by modifying the code or adding additional parameters.


function Restart-ExchangeServices {

    [CmdletBinding(DefaultParameterSetName = 'Default',
        ConfirmImpact = 'Medium',
        SupportsShouldProcess = $true,
        HelpUri = '')]
    [OutputType([string], ParameterSetName = 'Default')]

        [Parameter(ParameterSetName = 'Default',
            Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            ValueFromRemainingArguments = $true,
            HelpMessage = 'Enter the Name of the computer you would like to connect to.')]

        [Parameter(ParameterSetName = 'Default',
            Mandatory = $false,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = 'Enter computer name or pipe input'

    begin {

    process {
        foreach ($Computer in $ComputerName) {
            if ($PSCmdlet.ShouldProcess("$Computer", "Restarting Exchange Services")) {

                $Session = New-PsSession -ComputerName $Computer -Credential $Credential
                $Command = {
                    $Services = Get-Service | Where-Object -FilterScript { $_.Name -like "MSExchange*" -and $_.Status -eq "Running" }
                    foreach ($Service in $Services) {
                        Restart-Service $ -Force
                Invoke-Command -Session $Session -ScriptBlock $Command

    end {


Back to Top


Please feel free to copy parts of the script or if you would like to download the entire script, simple click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.

Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.


Back to Top