5 Windows administration tasks that can be automated using PowerShell

Windows admins can leverage a great deal of automation by integrating PowerShell (PS) into their daily tasks to make short work of even the most cumbersome administrative tasks.

Powershell

Image: iStock/BestForBest

Administrators have a tough, often tedious role to fill. Keeping everything going can be just as (if not more) difficult than swooping in to fix something that isn't working properly. Even relatively simple tasks, like creating new computer objects in Active Directory (AD) or ensuring that the devices on the network are all patched and operating optimally can be very time consuming when you factor in the sheer number of devices being managed.

SEE: TechRepublic Premium editorial calendar: IT policies, checklists, toolkits, and research for download (TechRepublic Premium)

I'm a big fan of automating as much as possible because it provides the following:

  • Removes much of the human error from repetitive tasks
  • Allows standardization to be implemented and adhered to
  • Frees up IT pros to attend to projects that require more focus

Lastly, it's better to work smarter, not harder, especially when it comes to types of repeat tasks that IT pros must perform. 

Here are a few tasks common to IT pros in the hopes that they help you to work smarter, while inspiring you to apply that thought process to your organization's needs and make the necessary changes to automate frequently accessed tasks for a simpler, more efficient workflow.

The cmdlets below reference variables, beginning with the "$" character. These are labeled with the data the variable should be holding to make the cmdlet execute without error. These must first be stated in your script as shown in the sample below when setting the OU path variable:

$OU=OU=Office,OU=Dept,OU=Company,DC=DomainName,DC=DomainExt

While the PowerShell cmdlets may be duplicated within your script, it is generally considered a best practice to keep the code to a minimum. This means eliminating duplicate code as much as possible. One way I've found that helps keep code nice and tidy, while making the script efficient, is to leverage a cmdlet such as Import-CSV and create a looping condition so that the cmdlet runs against all items within your CSV file with a column named "Computers" to create the desired effect, as shown in the example below when setting computer names:

Import-Csv -Path $csv | ForEach ( { New-ADComputer -Name "$($_.Computers)"

Creation of computer objects

ForEach ( { New-ADComputer -Name "$CompName" -Server $dc -Path $ou -Enabled $True } )

Add object to security groups

$comp = Get-ADComputer -Filter {Name -like $custom}

foreach ($station in $comp){Add-ADGroupMember -Identity $group -Members $station}

Obtain list of inactive objects

Get-ADComputer -SearchBase $searchOU -Filter {LastLogon -lt $time -and enabled -eq $true} -Properties LastLogon, description | Select-Object Name,DistinguishedName, description, enabled,@{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.LastLogon)}} | export-csv $logfile -notypeinformation

Perform Microsoft Updates

Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot | Out-File "C:\$Nodes-$(Get-Date -f yyyy-MM-dd)-MSUpdates.log" -Force

Determine VM Status and restart powered off servers

$VM = Get-SCVMHost -VMMServer "$VMMServer" -ComputerName "$CompName" | Get-SCVirtualMachine | Where-Object { $_.Status -eq "PowerOff" }

$VM | Start-SCVirtualMachine

Also see