customer relationships

Useful PowerShell Scripts/Commands

Step 1: Get-Help, Get-Command, Get-Member and Get-Alias

Thanks to Justin for pointing out that I left out probably the most useful command. Get-Help. I’ve added some of my other favorite commands for learning as well, which I still use almost daily.

Get-Help is an amazingly useful command and is quite versatile.

Get-Help Get-WMIObject – Will output basic help and tips on a command but Get-Help has many useful parameters or switches;

-Full (Is the full help document) 
-Examples (Shows only examples of how to use the command, my personal favourite) 
-Online (Opens the MSDN page for the cmdlet or command) 
-ShowWindow (This puts the help into its own window)

Get-Command is another great learning tool, think something might be a command? Use Get-Command to find out. It accepts wildcards as well. A great example is;

Get-Command -Name Get-* (May be a bit overwhelming looking but most of PowerShells cmdlets are pretty self explanatory) 
The most common verbs if you will, in my opinion anyway are;

Get- 
Show- 
New- 
Remove- 
Start- 
Stop- 
Copy- 
Import- 
Export-

You can use the above Get-Command example on any and all of those.

Get-Member is probably my second most used command when I’m writing a script or even making a one-liner (a one-liner is basically a one lined script)

It’s the tool to use if you want to see every single property, method, and basically everything that’s stored inside of a variable that you create(Please feel free to chime in if I’ve explained that incorrectly). Here’s an example;

$Object=Get-WMIObject -Class Win32_ComputerSystem 
$Object | Get-Member 
(I’d paste the output in here but it’s going to be formatted very poorly so I suggest you try it out yourself)

Get-Alias is great for when you’re trying to search for a solution and come across one but the author is using what I call shorthand for commands which is great if you already know it. An example would be gwmi, if you’re unfamiliar with PowerShell that probably won’t mean anything to you, that’s where Get-Alias comes in. Here’s an example.

Get-Alias -Name gwmi

The output of that command will tell you that gwmi is an alias of the command Get-WMIObject

Step 2: Quickly get the Computer Name, Model, Make, and other useful information

Get-WMIObject -Class Win32_ComputerSystem 
information about the System

Get-WMIObject -Class Win32_BIOS 
Information about the BIOS

Get-WMIObject -Class Win32_Baseboard 
Information about the Motherboard

Get-WMIObject -Class Win32_Processor 
Information about the CPU

Get-WMIObject -Class Win32_LogicalDisk 
Information about Logical Drives (Includes mapped drives and I believe PSDrives)

Get-WMIObject -Class Win32_DiskDrive 
Information about Physical Drives

Get-WMIObject -Class Win32_PhysicalMemory 
Information about the Memory

Get-WMIObject -Class Win32_NetworkAdapter 
Information about the NIC

Get-WMIObject -Class Win32_NetworkAdapterConfiguration 
Information about the NIC’s Configuration

You can also shortform the command from; 
Get-WMIObject -Class Win32_ComputerSystem into gwmi Win32_ComputerSystem

Note – Some may not know, I didn’t know at first but PowerShell is NOT case sensitive with the exception of a few special cases, from what I recall. I just like how neat it looks.

Step 3: Check your PowerShell Version

$PSVersionTable

To the best of my knowledge the important bit you’re looking for is the “PSVersion”.

Step 4: Restart all Network Adapters *Must be run as admin or at least local admin*

Requires PowerShell 3.0+

Get-NetAdapter | Restart-NetAdapter

Step 5: You can browse UNC path with PowerShell

Unless I’ve been doing something wrong, you cannot browse UNC with command prompt.

To access UNC via PowerShell simply type;

cd \\servername\C$\Path\To\File

You will of course need the appropriate permissions to do this, just like in Explorer.

Step 6: Copy a file to all users Desktop’s

I know you can do this with GPO but if you need to do it quickly or just a one off, or for specific people this usually does the trick.

$Users = Get-ChildItem C:\Users\ -Exclude “Administrator”,”Public”,”Default*” # Exclude any other defaults that you don’t want.

foreach($User in $Users.name){ 
$Path = “C:\Users\$User\Desktop”; 
Copy-Item -Path “\\Path\To\Source\File.txt” -Destination $Path\File.txt 
}

Step 7: Function for adding a system restart with a prompt for users.

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.VisualBasic”)

Function RestartPrompt { 
$Prompt1 = [Microsoft.VisualBasic.Interaction]::MsgBox(“IT is rebooting your machine. Please save and close any open work, if you wish to cancel the reboot please hit cancel.”, “OKCancel,Information”,”IT”)

if ($Prompt1 -eq “Ok”) { 
[Microsoft.VisualBasic.Interaction]::MsgBox(“Reboot will occur in 5 minutes.”, “OkOnly,Information”,”IT”) 
shutdown /r /t 300 
}

if ($Prompt1 -eq “Cancel”) { 
[Microsoft.VisualBasic.Interaction]::MsgBox(“Reboot has been canceled.”, “OKOnly,Information”,”IT”) 
break 

}

RestartPrompt

Step 8: Get free disk space on drives

This can either be run locally or part of a larger script to hit multiple machines.

$Drive=Get-WmiObject Win32_LogicalDisk -Filter “DriveType = 3” 
$DriveSize=$Drive.Size;$DriveSize=[math]::Round($DriveSize/1GB) 
$FreeSpace=$Drive.FreeSpace;$FreeSpace=[math]::Round($FreeSpace/1GB) 
$DriveName=$Drive.Name 
$ComputerName=Get-WmiObject Win32_ComputerSystem;$ComputerName=$ComputerName.Name 
$UsedSpace=$DriveSize – $FreeSpace;$UsedSpace=[string]$UsedSpace+” GB free on drive $DriveName on computer $ComputerName”

Step 9: Monitor Windows Server Backup

I’m quite proud of this, it’s worked well for me for a while and hopefully someone else can use it.

This works on Windows Server 2008R2+ and needs the Windows Server Backup feature installed along with the command line tools. I believe the command line tools comes as part of the feature in Server 2012.

You can set up the use of SSL and credentials if you choose to as well.

$Date=Get-Date; $Date=$Date.AddDays(-1).ToShortDateString() 
$WBS=Get-WBSummary 
$LastBackupResult=$WBS.LastBackupResultDetailedHR 
$LastSuccessfulBackup=$WBS.LastSuccessfulBackupTime.ToShortDateString() 
$LastBackup=$WBS.LastBackupTime 
$Versions=$WBS.NumberOfVersions 
$ErrorDesc=Get-WBJob -Previous 1; $ErrorDesc=$ErrorDesc.ErrorDescription

$SuccessfulBackupBody=@” 
Date: $LastSuccessfulBackup 
Backups Available: $Versions 
“@

$FailedBackupBody=@” 
Last Backup: Failed 
Date: $LastBackup 
Reason: $ErrorDesc 
“@

$RecurringFailedBackupBody=@” 
Last Backup: Failed 
Date: $LastBackup 
Last Successful Backup: $LastSuccessfulBackup 
Reason: $ErrorDesc 
“@

if($LastBackupResult -eq 0){ 
Send-MailMessage -To “recepient@email.com” -From “sender@email.com” -Subject “Backup Successful – $ENV:COMPUTERNAME” -Body $SuccessfulBackupBody -SmtpServer YourSMTPServer 

elseif($LastBackupResult -ne 0 -or $LastSuccessfulBackup -lt $Date){ 
if($LastSuccessfulBackupTime -lt $Date){ 
Send-MailMessage -To “recepient@email.com” -From “sender@email.com” -Subject “Backup Failed – $ENV:COMPUTERNAME” -Body $RecurringFailedBackupBody -SmtpServer YourSMTPServer 

else{ 
Send-MailMessage -To “recepient@email.com” -From “sender@email.com” -Subject “Backup Failed – $ENV:COMPUTERNAME” -Body $FailedBackupBody -SmtpServer YourSMTPServer 

}

Well hopefully these were helpful to some people and maybe a few people got to learn something new.


Got a project that needs expert IT support?

From Linux and Microsoft Server to VMware, networking, and more, our team at CR Tech is here to help.

Get personalized support today and ensure your systems are running at peak performance or make sure that your project turns out to be a successful one!

CONTACT US NOW
one click social media designs