{"id":2759,"date":"2019-05-01T19:35:35","date_gmt":"2019-05-01T19:35:35","guid":{"rendered":"http:\/\/microsoftgeek.com\/?p=2759"},"modified":"2019-05-01T19:38:09","modified_gmt":"2019-05-01T19:38:09","slug":"useful-powershell-scripts-commands","status":"publish","type":"post","link":"https:\/\/microsoftgeek.com\/?p=2759","title":{"rendered":"Useful PowerShell Scripts\/Commands"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Step 1: Get-Help, Get-Command, Get-Member and Get-Alias<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Thanks to Justin for pointing out that I left out probably the most useful command. Get-Help. I&#8217;ve added some of my other favorite commands for learning as well, which I still use almost daily.<\/p>\n\n\n\n<p>Get-Help is an amazingly useful command and is quite versatile.<\/p>\n\n\n\n<p>Get-Help Get-WMIObject &#8211; Will output basic help and tips on a command but Get-Help has many useful parameters or switches;<\/p>\n\n\n\n<p>-Full (Is the full help document)&nbsp;<br>-Examples (Shows only examples of how to use the command, my personal favourite)&nbsp;<br>-Online (Opens the MSDN page for the cmdlet or command)&nbsp;<br>-ShowWindow (This puts the help into its own window)<\/p>\n\n\n\n<p>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;<\/p>\n\n\n\n<p>Get-Command -Name Get-* (May be a bit overwhelming looking but most of PowerShells cmdlets are pretty self explanatory)&nbsp;<br>The most common verbs if you will, in my opinion anyway are;<\/p>\n\n\n\n<p>Get-&nbsp;<br>Show-&nbsp;<br>New-&nbsp;<br>Remove-&nbsp;<br>Start-&nbsp;<br>Stop-&nbsp;<br>Copy-&nbsp;<br>Import-&nbsp;<br>Export-<\/p>\n\n\n\n<p>You can use the above Get-Command example on any and all of those.<\/p>\n\n\n\n<p>Get-Member is probably my second most used command when I&#8217;m writing a script or even making a one-liner (a one-liner is basically a one lined script)<\/p>\n\n\n\n<p>It&#8217;s the tool to use if you want to see every single property, method, and basically everything that&#8217;s stored inside of a variable that you create(Please feel free to chime in if I&#8217;ve explained that incorrectly). Here&#8217;s an example;<\/p>\n\n\n\n<p>$Object=Get-WMIObject -Class Win32_ComputerSystem&nbsp;<br>$Object | Get-Member&nbsp;<br>(I&#8217;d paste the output in here but it&#8217;s going to be formatted very poorly so I suggest you try it out yourself)<\/p>\n\n\n\n<p>Get-Alias is great for when you&#8217;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&#8217;re unfamiliar with PowerShell that probably won&#8217;t mean anything to you, that&#8217;s where Get-Alias comes in. Here&#8217;s an example.<\/p>\n\n\n\n<p>Get-Alias -Name gwmi<\/p>\n\n\n\n<p>The output of that command will tell you that gwmi is an alias of the command Get-WMIObject<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Quickly get the Computer Name, Model, Make, and other useful information<\/h2>\n\n\n\n<p>Get-WMIObject -Class Win32_ComputerSystem&nbsp;<br>information about the System<\/p>\n\n\n\n<p>Get-WMIObject -Class Win32_BIOS&nbsp;<br>Information about the BIOS<\/p>\n\n\n\n<p>Get-WMIObject -Class Win32_Baseboard&nbsp;<br>Information about the Motherboard<\/p>\n\n\n\n<p>Get-WMIObject -Class Win32_Processor&nbsp;<br>Information about the CPU<\/p>\n\n\n\n<p>Get-WMIObject -Class Win32_LogicalDisk&nbsp;<br>Information about Logical Drives (Includes mapped drives and I believe PSDrives)<\/p>\n\n\n\n<p>Get-WMIObject -Class Win32_DiskDrive&nbsp;<br>Information about Physical Drives<\/p>\n\n\n\n<p>Get-WMIObject -Class Win32_PhysicalMemory&nbsp;<br>Information about the Memory<\/p>\n\n\n\n<p>Get-WMIObject -Class Win32_NetworkAdapter&nbsp;<br>Information about the NIC<\/p>\n\n\n\n<p>Get-WMIObject -Class Win32_NetworkAdapterConfiguration&nbsp;<br>Information about the NIC&#8217;s Configuration<\/p>\n\n\n\n<p>You can also shortform the command from;&nbsp;<br>Get-WMIObject -Class Win32_ComputerSystem into gwmi Win32_ComputerSystem<\/p>\n\n\n\n<p>Note &#8211; Some may not know, I didn&#8217;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.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Check your PowerShell Version<\/h2>\n\n\n\n<p>$PSVersionTable<\/p>\n\n\n\n<p>To the best of my knowledge the important bit you&#8217;re looking for is the &#8220;PSVersion&#8221;.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Restart all Network Adapters *Must be run as admin or at least local admin*<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Requires PowerShell 3.0+<\/p>\n\n\n\n<p>Get-NetAdapter | Restart-NetAdapter<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: You can browse UNC path with PowerShell<\/h2>\n\n\n\n<p>Unless I&#8217;ve been doing something wrong, you cannot browse UNC with command prompt.<\/p>\n\n\n\n<p>To access UNC via PowerShell simply type;<\/p>\n\n\n\n<p>cd \\\\servername\\C$\\Path\\To\\File<\/p>\n\n\n\n<p>You will of course need the appropriate permissions to do this, just like in Explorer.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 6: Copy a file to all users Desktop&#8217;s<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>$Users = Get-ChildItem C:\\Users\\ -Exclude &#8220;Administrator&#8221;,&#8221;Public&#8221;,&#8221;Default*&#8221; # Exclude any other defaults that you don&#8217;t want.<\/p>\n\n\n\n<p>foreach($User in $Users.name){&nbsp;<br>$Path = &#8220;C:\\Users\\$User\\Desktop&#8221;;&nbsp;<br>Copy-Item -Path &#8220;\\\\Path\\To\\Source\\File.txt&#8221; -Destination $Path\\File.txt&nbsp;<br>}<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 7: Function for adding a system restart with a prompt for users.<\/h2>\n\n\n\n<p>[System.Reflection.Assembly]::LoadWithPartialName(&#8220;Microsoft.VisualBasic&#8221;)<\/p>\n\n\n\n<p>Function RestartPrompt {&nbsp;<br>$Prompt1 = [Microsoft.VisualBasic.Interaction]::MsgBox(&#8220;IT is rebooting your machine. Please save and close any open work, if you wish to cancel the reboot please hit cancel.&#8221;, &#8220;OKCancel,Information&#8221;,&#8221;IT&#8221;)<\/p>\n\n\n\n<p>if ($Prompt1 -eq &#8220;Ok&#8221;) {&nbsp;<br>[Microsoft.VisualBasic.Interaction]::MsgBox(&#8220;Reboot will occur in 5 minutes.&#8221;, &#8220;OkOnly,Information&#8221;,&#8221;IT&#8221;)&nbsp;<br>shutdown \/r \/t 300&nbsp;<br>}<\/p>\n\n\n\n<p>if ($Prompt1 -eq &#8220;Cancel&#8221;) {&nbsp;<br>[Microsoft.VisualBasic.Interaction]::MsgBox(&#8220;Reboot has been canceled.&#8221;, &#8220;OKOnly,Information&#8221;,&#8221;IT&#8221;)&nbsp;<br>break&nbsp;<br>}&nbsp;<br>}<\/p>\n\n\n\n<p>RestartPrompt<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 8: Get free disk space on drives<\/h2>\n\n\n\n<p>This can either be run locally or part of a larger script to hit multiple machines.<\/p>\n\n\n\n<p>$Drive=Get-WmiObject Win32_LogicalDisk -Filter &#8220;DriveType = 3&#8221;&nbsp;<br>$DriveSize=$Drive.Size;$DriveSize=[math]::Round($DriveSize\/1GB)&nbsp;<br>$FreeSpace=$Drive.FreeSpace;$FreeSpace=[math]::Round($FreeSpace\/1GB)&nbsp;<br>$DriveName=$Drive.Name&nbsp;<br>$ComputerName=Get-WmiObject Win32_ComputerSystem;$ComputerName=$ComputerName.Name&nbsp;<br>$UsedSpace=$DriveSize &#8211; $FreeSpace;$UsedSpace=[string]$UsedSpace+&#8221; GB free on drive $DriveName on computer $ComputerName&#8221;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 9: Monitor Windows Server Backup<\/h2>\n\n\n\n<p>I&#8217;m quite proud of this, it&#8217;s worked well for me for a while and hopefully someone else can use it.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>You can set up the use of SSL and credentials if you choose to as well.<\/p>\n\n\n\n<p>$Date=Get-Date; $Date=$Date.AddDays(-1).ToShortDateString()&nbsp;<br>$WBS=Get-WBSummary&nbsp;<br>$LastBackupResult=$WBS.LastBackupResultDetailedHR&nbsp;<br>$LastSuccessfulBackup=$WBS.LastSuccessfulBackupTime.ToShortDateString()&nbsp;<br>$LastBackup=$WBS.LastBackupTime&nbsp;<br>$Versions=$WBS.NumberOfVersions&nbsp;<br>$ErrorDesc=Get-WBJob -Previous 1; $ErrorDesc=$ErrorDesc.ErrorDescription<\/p>\n\n\n\n<p>$SuccessfulBackupBody=@&#8221;&nbsp;<br>Date: $LastSuccessfulBackup&nbsp;<br>Backups Available: $Versions&nbsp;<br>&#8220;@<\/p>\n\n\n\n<p>$FailedBackupBody=@&#8221;&nbsp;<br>Last Backup: Failed&nbsp;<br>Date: $LastBackup&nbsp;<br>Reason: $ErrorDesc&nbsp;<br>&#8220;@<\/p>\n\n\n\n<p>$RecurringFailedBackupBody=@&#8221;&nbsp;<br>Last Backup: Failed&nbsp;<br>Date: $LastBackup&nbsp;<br>Last Successful Backup: $LastSuccessfulBackup&nbsp;<br>Reason: $ErrorDesc&nbsp;<br>&#8220;@<\/p>\n\n\n\n<p>if($LastBackupResult -eq 0){&nbsp;<br>Send-MailMessage -To &#8220;<a href=\"mailto:recepient@email.com\">recepient@email.com<\/a>&#8221; -From &#8220;<a href=\"mailto:sender@email.com\">sender@email.com<\/a>&#8221; -Subject &#8220;Backup Successful &#8211; $ENV:COMPUTERNAME&#8221; -Body $SuccessfulBackupBody -SmtpServer YourSMTPServer&nbsp;<br>}&nbsp;<br>elseif($LastBackupResult -ne 0 -or $LastSuccessfulBackup -lt $Date){&nbsp;<br>if($LastSuccessfulBackupTime -lt $Date){&nbsp;<br>Send-MailMessage -To &#8220;<a href=\"mailto:recepient@email.com\">recepient@email.com<\/a>&#8221; -From &#8220;<a href=\"mailto:sender@email.com\">sender@email.com<\/a>&#8221; -Subject &#8220;Backup Failed &#8211; $ENV:COMPUTERNAME&#8221; -Body $RecurringFailedBackupBody -SmtpServer YourSMTPServer&nbsp;<br>}&nbsp;<br>else{&nbsp;<br>Send-MailMessage -To &#8220;<a href=\"mailto:recepient@email.com\">recepient@email.com<\/a>&#8221; -From &#8220;<a href=\"mailto:sender@email.com\">sender@email.com<\/a>&#8221; -Subject &#8220;Backup Failed &#8211; $ENV:COMPUTERNAME&#8221; -Body $FailedBackupBody -SmtpServer YourSMTPServer&nbsp;<br>}&nbsp;<br>}<\/p>\n\n\n\n<p>Well hopefully these were helpful to some people and maybe a few people got to learn something new.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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 &#8211; Will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[],"class_list":["post-2759","post","type-post","status-publish","format-standard","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/2759","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2759"}],"version-history":[{"count":3,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/2759\/revisions"}],"predecessor-version":[{"id":2764,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/2759\/revisions\/2764"}],"wp:attachment":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}