Doing more with less is a common mantra bandied about in the workforce these days and IT is no exception. If you’re part of a small team that’s responsible for many devices, the ability to leverage PowerShell against hundreds or thousands of systems will make you feel like you’ve added several talented IT pros to the team.
So without further ado, let’s review the requirements necessary to get the most out of PowerShell’s awesome features. Then we’ll focus on 10 commands that will make life easier by managing devices and services on your network remotely and more efficiently.
Requirements
- Computer running Windows Vista (or higher)
- Server running Windows Server 2008 (or higher)
- PowerShell 5.0
- Administrative access
1: Create a PowerShell session
Command: Enter-PSSession
Example: Enter-PSSession -ComputerName REMOTE_COMPUTER_NAME -Credential USERNAME
Creating a PSSession will allow an administrator to remotely connect to a computer on the network and run any number of PS commands on the device. During the session, multiple commands may be executed remotely, since the admin has console access just as though he/she were sitting locally at the machine.
2: Execute commands
Command: Invoke-Command
Example: Invoke-Command -Computer REMOTE_COMPUTER_NAME -ScriptBlock {PowerShell Command}
Using Invoke-Command in PS renders similar results to executing a session as in command #1 above, except that when using Invoke to call forth a command remotely, only one command may be executed at a time. This prevents running multiple commands together unless they are saved as a .PS1 file and the script itself is invoked.
3: Restart computer(s)
Command: Restart-Computer
Example: Restart-Computer -ComputerName REMOTE_COMPUTER_NAME -Force
Sometimes installations or configurations will require a reboot to work properly. Other times, a computer just needs a refreshing of the resources, and a reboot will accomplish that. Whether targeted at one or one hundred devices, PS can ease the job with just one command for all.
4: Ping computer(s)
Command: Test-Connection
Example: Test-Connection -ComputerName DESTINATION_COMPUTER_NAME -Source SOURCE_COMPUTER_NAME
The PING command is one of the most useful commands in a sysadmin’s arsenal. Simply put, it tests connectivity between your current station and another remote system. Test-Connection brings it up a notch by folding that functionality into a PS cmdlet, while adding some new tricks–such as being able to designate a source computer that’s different from the one you’re currently logged onto. Say you need to test communications between a server and a remote device. The ICMP requests will be sent from the server to the remote device, yet report the findings back to your admin station.
5: View and modify services
Command: Set-Service
Example: Set-Service -ComputerName REMOTE_COMPUTER_NAME -Name SERVICE_NAME -Status SERVICE_STATUS
Services are resilient and sometimes finicky. Depending on what’s going on with a particular computer, they may halt at the worst possible time. Determining a station’s running services begins with the Get-Service cmdlet to obtain current statuses. Once that information is available, the process to set a service status is possible – be it for one service, those that begin with the letter W, or all of them at once.
6: Run background tasks
Command: Start-Job
Example: Start-Job -FilePath PATH_TO_SCRIPT.PS1
Some administrators do what they need to do when they need to do it, regardless of what’s going on or what the users are doing. Others prefer to work in the shadows to keep things humming along with little to no interruptions. If you’re one of the latter, this cmdlet is perfect for your management style.
It executes scripts or tasks in the background no matter who is interactively logged on or what they may be doing. Further, it will execute silently–even if it were to fail–and not interrupt the locally logged on user at all. Like a ghost!
7: Shut down computer(s)
Command: Stop-Computer
Example: Stop-Computer -ComputerName REMOTE_COMPUTER_NAME -Force
Unlike running things silently or rebooting a desktop from afar, there are times when computers need to be shut down. For these moments, this cmdlet will ensure that one or all computers are properly shut down and will even log off interactive users if the -Force argument is included.
8: Join computers to a domain
Command: Add-Computer
Example: Add-Computer -ComputerName COMPUTER_NAMES_TO_BE_JOINED -DomainName DOMAIN.COM -Credential DOMAIN\USER -Restart
While the process of joining a computer to a domain is fairly straightforward, the three clicks and entering of admin credentials can become quite tedious when multiplied by several hundreds of computers at a time.
PowerShell can make short work of the task. This cmdlet allows for multiple computers at once to be joined to a domain, while requiring the admin to enter his/her credentials only once.
9: Manage other applications and services
Command: Import-Module
Example: Import-Module -Name NAME_OF_POWERSHELL_MODULE
One of PowerShell’s greatest benefits is its flexibility when it comes to managing just about anything–from Windows-based computing systems to applications like Microsoft Exchange. Some applications and system-level services permit only a certain level of management via GUI. The rest is defaulted to PS, so Microsoft is clearly leveraging the technology significantly.
This is accomplished through the use of modules that contain the necessary codebase to run any number of additional cmdlets within PowerShell that target a specific service or application. Modules may be used only when needed by importing them, at which point they will extend the PS functionality to a specific service or app. Once your work is done, you can remove the module from the active session without closing it altogether.
10: Rename computers
Command: Rename-Computer
Example: Rename-Computer -NewName NEW_COMPUTER_NAME -LocalCredential COMPUTERNAME\USER -Restart
Depending on several factors, including the deployment system used, scripting experience level and security, and company policy, computers being renamed might not be done regularly (or perhaps it’s a task performed quite often). Either way, the Rename cmdlet is extremely useful when working on one or multiple systems–workgroup or on a domain.
The cmdlet will rename a device and reboot it so that the changes can take effect. For those on a domain, the added benefit will be that if the Active Directory Schema supports it, the new computer will also result in a computer object rename within AD. The object will retain all its settings and domain joined status but will reflect the new name without any significant downtime to the user outside of a reboot.
Your take
What are some of your favorite PowerShell commands? Which ones do you utilize on a regular basis to manage your environment(s) best? Sound off in the comments section below and share your advice with fellow TechRepublic members.
PowerShell how’s and why’s…
- How to use PowerShell modules to knock out your admin chores
- How to automate account pre-staging in WDS with PowerShell
- How to switch between GUI and Core in Windows Server 2012 using PowerShell
- 10 reasons why you should learn to use PowerShell