Windows Server 2022 & Powershell All-in-One For Dummies
Book image
Explore Book Buy On Amazon
PowerShell 5.1 is the version of Windows PowerShell that ships with Windows Server 2022, Windows Server 2019, and Windows Server 2016. It’s available for installation on Windows Server 2008 R2 with Service Pack1, Windows Server 2012, and Windows Server 2012 R2. The last three operating systems must have Windows Management Framework 5.1 installed to support PowerShell 5.1.

You can upgrade to PowerShell 7.2 fairly easily (the more recent version from Microsoft), though the examples on this Cheat Sheet were only tested in PowerShell 5.1.

Common administrative tasks

Most system administrators have some commands that they find useful on a daily basis. Here are some you’re sure to need.

Setting up for remote management

For setting up remote management on systems, there are two commands you need to be aware of.

To start the WinRM service and create exceptions in the Windows Firewall, run the following:

winrm quickconfig

To configure the system to accept remote PowerShell commands via WS-MAN, run the following:

Enable-PSRemoting

Disabling sconfig at startup on Windows Server Core

If you’ve worked with Windows Server Core, you’re probably familiar with sconfig, the configuration utility. It’s the first screen you see after you log in to a Windows Server Core system. You may want to disable this menu from coming up after login, and you can do so by typing the following and pressing the Enter key:

Set-SConfig -AutoLaunch $false

That’s all there is to it! If you change your mind and you decide you want it to launch again after login, simply type the following and press Enter:

Set-SConfig -AutoLaunch $true

Using environmental variables

To view available environmental variables, run the following:

Set | More

To add a folder named Tools to a path variable so you can run those tools without having to be in that directory, run the following:

SET PATH=%PATH%;C:\Tools

To view your current path variable settings, type the following:

echo %PATH%

Enabling/disabling the firewall

Here’s the command to disable the firewall for all profiles:

Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False

To re-enable the firewall for all profiles, use the following:

Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True

Working with containers

Containers are the new technology trend, especially as the world shift toward microservices. Here are some helpful Docker commands:

  • docker login: Logs you into a DockerHub repository.
  • docker pull mcr.microsoft.com/windows/nanoserver:ltsc2022: Retrieves container images. In this case, it pulls from the mcr.microsoft.com/windows repository and retrieves the nanoserver image with the tag ltsc2022.

The nanoserver container images no longer use the latest tag so you’ll have to specify which container image tag you want. You can look up available tags on the container image’s page on Docker Hub.

  • docker ps: Shows all running containers. To view all containers, including those not running, simply add -a to the end of the command.
  • docker build <dockerfile>: Builds a container from a Dockerfile.
  • docker run: Starts a container utilizing various arguments.
  • docker volume create: Creates a named volume.
  • docker container prune: Removes stopped containers.

Troubleshooting with the command prompt

 

Name Command Description
System File Checker sfc /scannow This utility checks system files to see if they match what’s expected by comparing the signature of the system file on the server with the signature of a cached copy of the same file. The cached files are stored in a compressed folder located at C:\Windows\System32\dllcache. If a corrupt system file is found, it’s replaced.
Check Disk chkdsk /f /r This utility repairs file system errors and marks bad sectors so the operating system doesn’t use them anymore. The /f tells the utility to fix any issues it finds, and the /r locates the bad sectors (areas) on the disk. This can take a while. Kick it off, and grab a cup of coffee.
Driverquery driverquery This utility queries the system for all the hardware drivers that are installed on Windows. This can be very helpful if you’re running into issues with systems that have similar hardware and you want to know if they have a driver in common.
BCDEdit bcdedit Allows you to edit the boot configuration on your Windows server.

 

About This Article

This article is from the book:

About the book author:

Sara Perrott is an information security professional with a systems and network engineering background. She teaches classes related to Windows Server, Amazon Web Services, networking, and virtualization. Sara addressed the AWS Imagine conference in 2018 and presented at the RSA conference in 2019.

This article can be found in the category: