Labels

Thursday 17 January 2019

PowerShell Quick Book For IT Admins

Microsoft PowerShell is a great scripting platform.  One of the biggest advantages of using PowerShell is that you do not need to write several lines of code to accomplish a simple task. Traditionally, you had to write many lines of code in VB scripting language to accomplish simple tasks. For example, if you need to check services on Windows computers and save the output to a file, you will write several lines of code that includes writing a method to open a text file and then use another line of code to save the output to the text file. PowerShell has changed the way scripting works. Microsoft has put a lot of effort into ensuring administrators can work with PowerShell scripting without acquiring much knowledge on the scripting. In this article, I am trying to share some useful PowerShell one-liner commands that can be helpful in daily use.

Saving Output To A File


In traditional work of programming we were using lots of lines of code to save the output of script. Microsoft converted that tedious way of programming to very easy single command through power shell. Now we can use "Export-CSV" command at the end of any powershell command and the out will come into the the separate file. For example, in the PowerShell one-liner command below, you are collecting the status of Windows services on the local computer, but rather than showing the output in the PowerShell screen window you are saving the output to a CSV file. For example, these commands save the output to a CSV file:


Example 1: Export Service information in file

Get-Service | Export-CSV C:\Temp\AllServices.CSV –NoTypeInfo
Example 2: Find user detail name John and saving in CSV file
Get-User –Filter ‘Name –Like “*John”’ | Export-CSV C:\Temp\AllUsers.CSV –NoTypeInfo

Example 3: Exporting all child item details under folder C:\Windows\System32 
Get-ChildItem –Path C:\Windows\System32 | Export-CSV C:\Temp\AllFiles.CSV -NoTypeInfo

Checking Who Rebooted The Production Server


Let’s say one of the production servers got rebooted unexpectedly and you would like to find out who rebooted it and when the server got rebooted. In PowerShell, you can take a look at the event log using the PowerShell one-liner command shown below. You don’t need to write a bunch of lines in a script and then run the script. Here is how you do it.


Get-EventLog –Log System –Newest 100 | Where-Object {$_.EventID –eq ‘1074’} | FT MachineName, UserName, TimeGenerated -AutoSize

The above command checks the System event log and searches for Event ID 1074 and then prints the machine name, username, and time the event got generated. If you would like to save the output to a CSV file, simply use Export-CSV cmdlet as shown in the command below:


Get-EventLog –Log System –Newest 100 | Where-Object {$_.EventID –eq ‘1074’} | FT MachineName, UserName, TimeGenerated –AutoSize | Export-CSV C:\Temp\AllEvents.CSV -NoTypeInfo

powershell one-liner


Checking to see if a KB is installed on a Windows machine


If you would like to search for a hotfix on a local or remote computer, you can use Get-HotFix PowerShell cmdlet as shown in this command:
Get-HotFix –ID KB2877616
And to query the hotfix information on the remote computer, simply add –Computername parameter as shown in the command below:
Get-HotFix –ID KB2877616 –Computername Test.rajatcitrix.com

Back up all production Group Policy Objects


As we stated earlier, PowerShell offers quick one-liner commands. If you would like to backup all production Group Policy Objects (GPOs) in an Active Directory environment, use Backup-GPO PowerShell cmdlet as it is highlighted in the command below:

Backup-GPO –All –Path C:\Temp\AllGPO

Check if all Domain Controllers are Global Catalog Servers


In most of the Active Directory production environments, all domain controller are designated as global catalog servers. The Global Catalog servers help in finding information in the Active Directory quickly. If you need to check whether all domain controllers are acting as global catalog servers or not, run this command:

Get-ADDomainController –Filter * | Select Hostname, IsGlobalCatalog

And to export the output to a CSV file, execute this command:

Get-ADDomainController –Filter * | Select Hostname, IsGlobalCatalog | Export-CSV C:\Temp\AllDomainControllerStatus.CSV -NoTypeInfo







No comments:

Post a Comment

Thanks for Messaging i will respond ASAP.

How To Build IT Operations Future Ready

 IT Operations is most critical piece in every organization. Without appropriate mindset, tools and policy  it's a nightmare for any org...