10 Useful PowerShell Commands

December 14, 2023

[avatar user="Jamie Poindexter" size="thumbnail" link="file" /]by Jamie Poindexter | Mar 21, 2022 | Jamie's Tech Corner, Our blogIf you are a IT admin you probably already use PowerShell. It’s the more powerful big brother to the command prompt. It’s a powerful tool to scripting and gathering information on a computer or ever Office 365. Here are a couple commands that could come in use the next time you are working on a computer. First steps:Before we begin, make sure you are running PowerShell as admin. If you are creating scripts to run on other systems, you may need to enable the execution of scripts beforehand. Setting the execution policy to remotesigned by running this command and choose Y for yes.Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

Text Description automatically generated

With that out of the way you are free to run the below scripts and commands.1.Download a file from PowerShell:Say you need to download Teams or another app from the internet to a batch of PCs. You would run the following command to download a URL of your choice and choose where to save the file. Optionally you could also execute it all in one command

Graphical user interface, text Description automatically generated with medium confidence

This will download the file to the desktop of the user that ran the command and then execute it. Adding a ; between the commands will run them one after another.2.Run Windows Update:Just as above, installing Windows updates on multiple at the same time can be time consuming. The below command will install the Nuget package provider and then install the Windows Update module. Once that’s install it will initiate Windows Update check and install anything it finds. Switching the “ignorereboot” with “autoremote” will let it automatically reboot when done.Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -ForceInstall-Module -Name PSWindowsUpdate -Force;Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -IgnoreReboot3.Connect to Another PC using PS:If you need to run PS commands on another PC on the network, you can use the command “Enter-PSSession Computer1”Just replace the Computer1 with the name or IP of the remote PC. A side note thought, the WinRM service needs to be started for this to work.4.Get info about the PC:If you need to gather info about the hardware or OS you can use the “Get-ComputerInfo” command. Optionally you can filter by adding a wildcard for what info you are needing. Such as “Get-ComputerInfo "*Proc*" to get info about the processor.

Graphical user interface, text Description automatically generated

5.Get Windows event logs:Pulling the Windows event logs can also be done through PS. Just replace the log name (system,application,security) and the number of events you want to retrieve.Get-EventLog -LogName System -Newest 5

A screenshot of a computer Description automatically generated
A screenshot of a computer Description automatically generated with medium confidence

6.Getting and ending processes:You can list all processes or search for a specific process as well as end any that are found.List Chrome - Get-Process -Name “Chrome”

Table Description automatically generated

You can then kill any that match the search using - Get-Process -Name “Chrome”| Stop-Process7.View password expiration for AD users:Run the following command on a DC to list all users that are enabled in AD and show the password expiration date.Get-ADUser -Filter 'enabled -eq $true' -Properties AccountExpirationDate | Select sAMAccountName, distinguishedName, AccountExpirationDate8.Checkdisk Scan from PS:Everyone already knows about the chkdsk command from CMD but did you know you can also do it from PS? Running the command “Repair-Volume -DriveLetter H -Scan” and replace the H with the drive letter of your choice. This will run a report only scan on that drive. You can also repair what is found and specify more than 1 driveRepair-Volume -DriveLetter GHI -SpotFixThe above command will scan the G,H and I drives and run a repair if anything is found.

Logo Description automatically generated

9.Connect to Office 365:PS not only works on local or remote computers but also Office 365 tenants. This opens the door to a whole other world of automation and scripts. First you will need to import the module to connect to the tenantImport-Module ExchangeOnlineManagementThen run the following command and replace the email with a admin user on the tenant.Connect-ExchangeOnline -UserPrincipalName navin@contoso.comThis works with MFA enabled accounts as well and you will be asked to sign in and provide a MFA code.10.Create a new O365 user:Once you are connected to the tenant as explained above you can create a new user using the following$PasswordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile$PasswordProfile.Password = “S3cureP@ssword”New-AzureADUSer -DisplayName “DISPLAYNAME” -PasswordProfile $PasswordProfile -UserPrincipalName “EMAILADDRESS” -AccountEnabled $trueMake sure to fill in the Display Name and Email Address/Principal NameThe user should then appear on the tenant and be enabled.