Powershell

How to use PowerShell Stop-Computer Command

In the ever-evolving world of technology, administrators often grapple with managing multiple computers simultaneously, especially in enterprise environments. To streamline this process, PowerShell, Microsoft’s powerful scripting language and automation framework, offers the “Stop-Computer” cmdlet as a versatile tool for remotely shutting down or restarting computers.

This article aims to provide a comprehensive analysis of the capabilities, commands, parameters, and real-world applications of the Stop-Computer cmdlet.

What is the Stop-Computer cmdlet?

Stop-Computer, belonging to the “Microsoft.PowerShell.Management” module, empowers administrators to control and halt remote computers. This cmdlet not only enables graceful shutdowns or reboots but also provides flexibility by allowing different authentication mechanisms. PowerShell can automate this task across multiple machines, thus significantly simplifying their workload.

Availability and Compatible Systems

Initially introduced in Windows PowerShell 3.0, the Stop-Computer cmdlet has since become an integral part of subsequent versions, including PowerShell 4.0 and the latest PowerShell 7.0. It operates seamlessly on Windows operating systems starting from Windows 7 and Windows Server 2008 R2, ensuring compatibility across a wide range of environments.

In PowerShell 7.1, the Stop-Computer function became available for Linux and macOS as well. These platforms are unaffected by the settings. The cmdlet is only executing “/sbin/shutdown”, which is a native command.

Syntax and Usage

The general syntax for the Stop-Computer cmdlet is as follows:

Stop-Computer [-ComputerName] [-AsJob] [-Force] [-Protocol {RemoteShutdown | WSMan}]

In the given syntax:

  • The “-ComputerName” parameter lets administrators remotely shut down or restart one or more targeted machines in the network.
  • The optional “-AsJob” parameter allows the cmdlet execution to be performed in the background, enhancing scalability and automation.
  • The “-Force” parameter forces the immediate shutdown of the specified computer. The method for user credential authentication is specified by the “WSMan” parameter.

Note: When administered without any parameters, the cmdlet targets the local machine, stopping it either for a restart or a shutdown.

Use Cases in System Administration

System administrators rely on the Stop-Computer cmdlet to perform scheduled maintenance operations across a network. By initiating coordinated shutdowns or restarts, they can execute patches, updates, or apply system-wide configuration changes without individually accessing each machine, saving considerable time and effort.

Example 1: Shutting Down the Local Computer

In this example, the Stop-Computer command shuts down the local computer:

Stop-Computer -ComputerName localhost

Example 2: Shutting Down Remote and Local Computers

In this specific example, the discussed command shuts down both the remote and local computers:

Stop-Computer -ComputerName "Server01", "Server02", "localhost"

The local computer, two distant computers, and ComputerName are all specified by the Stop-Computer command. It resultantly shuts down all the computers.

Example 3: Shutting Down Remote Computers from Background

In this example, two distant computers are running Stop-Computer as a background task. The Stop-Computer command is executed in the background via the background operator “&”:

$j = Stop-Computer -ComputerName "Server01", "Server02" &
$results = $j | Receive-Job
$results

In these commands:

  • Two distant machines are specified using the “ComputerName” parameter in the “Stop-Computer” command.
  • The command is executed in the background using the background operator “&”. The “$j” variable contains the job objects.
  • The job objects transferred down the pipeline to “Receive-Job,” which receives the task results, are stored in the “$j” variable. The “$results” variable contains the objects. The job information is displayed in the PowerShell console via the “$results” variable.

Example 4: Remote Management Capabilities

One of the most significant features of Stop-Computer is its potential to remotely command multiple computers within a network. This ability allows system administrators to save time and effort by performing tasks simultaneously on several machines, minimizing interruptive procedures:

Stop-Computer -ComputerName "Server01" -WsmanAuthentication Kerberos

The “ComputerName” option of the Stop-Computer command is used to identify the remote computer. In order to create a remote connection, Kerberos is to be used, according to the “WsmanAuthentication” parameter.

Conclusion

The Stop-Computer cmdlet serves as a crucial tool for system administrators, offering a way to remotely shut down or restart computers efficiently. PowerShell’s scripting and automation capabilities, combined with the power of this cmdlet, provide administrators with immense control over large networks, enhancing productivity and ensuring smooth operations.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.