30 basic Powershell commands to start with Windows Server

Estimated read time 4 min read

Getting started with Windows 2019 nano server can be very challenging especially if you are not used to CLI on Linux servers. I decided to install Windows 2019 server core only i.e; without GUI and take it as a challenge to learn more about it. Since I’m mostly from a Unix/Linux background, I decided to dive a little bit more in the Windows Operating system. In this article, I’m sharing some commands to start with. However, the Microsoft website covers enough part which will lead to the Windows 2016 MCSE certification. My goal in this article is to get PowerShell beginners on track and paint an idea of what Windows Powershell is capable of.

Windows Updates

1. Some modules are not available by default on the PowerShell. So you will need to manually download it. I downloaded the module PSWindowsUpdate which will enable me to update the OS from PowerShell.

Get-Module PSWindowsUpdate

2. One of the first things you might want to do is to get the updates that need to be installed on  the Operating System first which I did with the following command:

Get-WindowsUpdate

3. Now you can install the updates using the following command. Once installed, reboot the server which might take some time.

Install-WindowsUpdate

4. You can also find the list of updates installed on the machine using the following command:

Get-Hotfix

OS basic verification

5. To get the version of the PowerShell, use the following command:

  • Get-host | select Version
  • $PSVersionTable

6. To get the reboot history :

Get-EventLog system | where-object {$_.eventid -eq 6006} | select -last 10

7. List of services running:

Get-Service

8. List of Installed programs:

Get-Module PSWindowsUpdate
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize

9. Get the process running on Windows

Get-process

Networking

10. Finding the rules on the firewall:

Get-NetFirewallRule

11. IP configuration:

Get-NetIPConfiguration

12. To verify if IPv4 or IPv6 is enabled or not

Get-NetAdapterLso

13. To get the routing table

Get-NetRoute

14. Source and remote IPs on listening mode

Get-NetTCPConnection | ? {$_.State -eq "Listen"}

Storage

15. Get the disk space information

Get-WmiObject win32_logicaldisk
Get-PhysicalDisk

16. To get the health status of the disk:

Get-PhysicalDisk | Sort Size | FT FriendlyName, Size, MediaType, SpindleSpeed, HealthStatus, OperationalStatus -AutoSize

17. Getting the used/free space:

Get-PSDrive C | Select-Object Used, Free

PowerShell Modules and repository

18. The PowerShell Gallery is the central repository for PowerShell content. You can find new PowerShell commands or Desired State Configuration (DSC) resources in the Gallery. To check which repository you are using use the following :

get-PSRepository

19. By default, PowerShell modules are installed in several directories. You can download certain modules in any directory you want. To execute it, you need to append the environment. To see the module paths of your environment, launch the following command:

$Env:PSModulePath

20. To add a path to the environment’s module path, use the following command:

$env:PSModulePath = $env:PSModulePath + ";C:\ModulePath"

21. To get a list of installed modules and the directories in which it has been installed:

Get-Module -ListAvailable

22. To install a module, you can search for it and install it directly:

find-module -Name PendingReboot | install-module 

23. After installing a module, it is always a good practice to import it using the following:

Import-Module PendingReboot

24. To get information which command to use for a pending reboot use:

Get-Command -Module PendingReboot

Downloads and unzip

25. Recently, I saw a link where having a script on Technet. You can use the following command to download the zip file.

Invoke-WebRequest https://gallery.technet.microsoft.com/scriptcenter/Get-RebootHistory-bc804819/file/130620/2/Get-RebootHistory.zip -outfile Get-RebootHistory.zip

26. To unzip a file:

Expand-Archive -path '.\Get-RebootHistory.zip' -DestinationPath 'C:\Users\Administrator\Documents\WindowsPowerShell\Modules'

Getting Help

27. Getting help with commands arguments are pretty easy. Let’s say you want to know the possible arguments of the command Get-Command, simply do:

Get-Command -?

28.  You can also use the Get-Help module. For example, consider the Compress-Archive module:

Get-Help -name Compress-Archive

29. Consider that you need an example of a specific module:

Get-Help -name Compress-Archive -examples

30. The help/man command is also interesting to get fast syntax about a command.

man Compress-Archive
help Compress-Archive

One interesting source of information is gallery.technet.microsoft.com/scriptcenter where you can access several scripts for Microsoft Windows Server Administration. Personally, Windows PowerShell commands seem to be more complexed compared to Linux commands. Probably, its a matter of getting used to it. I will try my best to update this article. Don’t forget to comment below if needed. My last article on Windows was on the installation of SSH on Windows server through Powershell.

Nitin J Mutkawoa https://tunnelix.com

Blogger at tunnelix.com | Founding member of cyberstorm.mu | An Aficionado Journey in Opensource & Linux – And now It's a NASDAQ touch!

You May Also Like

More From Author