Posts

Showing posts with the label Powershell

Powershell Script: Validate if Computer account exists in Active Directory

The below script will let you quickly validate if given computers are exists in Active Directory. You have to keep both script and Computer.txt in same location. Add the computers name in text file separated by new line. The result will be saved in Output.csv file in script folder. $File = "$PSScriptRoot\Computers.txt"  $LogFile = "$PSScriptRoot\Output.csv"    # Function to WriteLog File  Function WriteLog($Msg)  {      $Text = $Msg      Add-Content -Path $LogFile  $text       Write-Host $Text  }      $ComputerList = Get-Content -Path $File  $TotalRecord = ($ComputerList).Count  foreach ($computer in $ComputerList){      $error.clear()      try{$ADComputer = Get-ADComputer $computer -Properties *}      catch{ WriteLog "$computer,Does not Exists in AD"}    ...

Powershell - Add Users / Computers to AD Group

The below script can be used to add multiple user or computer to an AD group. The list of users / computers to be provided in the text file. Usage: See below example. To see full help Type script name and press enter. You have to provide computer name or user name (SAM Account) in text file. Add computer to AD Group     .\AddObjectsToADGroup.ps1 -FileName Computers.txt -ADGroupName "TestGroup" -ObjectType Computer .Add user to AD group     .\AddObjectsToADGroup.ps1 -FileName Computers.txt -ADGroupName "TestGroup" -ObjectType User Github Repostiorty Raw File #Script #Version: 1.0 #Author: Equebal Ahmad <# .SYNOPSIS     Add the computers / users account to an AD Group .DESCRIPTION     Add the computers / users account to an AD Group     The script can add user and computer in an AD group. You need to pass this with ObjectType parameter. .EXAMPLE     .\AddObjectsToADGrou...

Powershell - Get system uptime

$Computer = Read-Host   "Input Computer Name " $LastBoot = (Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer ).LastBootUpTime $uptime = (Get-Date) - [System.Management.ManagementDateTimeconverter]::ToDateTime($LastBoot) Write-Host  "$Computer uptime is:"     [$uptime]   [dd:hh:mm:ss.ms]

Powershell - List AD Organizational Unit and GPOs linked to them

The below script will search Active Directory for all Organizational Unit which contain specific name and list them along with all Group Policies linked to those OUs. Example: The below command will get a list of all OUs which name contains 'Test'. It will also show the details of all GPOs linked to OUs. Usage Example: .\Get-OUList.ps1 –OUName “Test” #Script [ CmdletBinding () ] Param (   [ Parameter (Mandatory = $True ) ]   [ string ] $OUName   ) $invocation = ( Get-Variable MyInvocation ) . Value $directorypath = Split-Path $invocation . MyCommand . Path $outputfile = $directorypath + "\Result.csv" $OUName = "*" + $OUName + "*" $Results = @() $OUList = Get-ADOrganizationalUnit -Filter * | Where-Object -FilterScript { $PSItem . distinguishedname -like $OUName } foreach ( $OU in $OUList ){     $LinkedGPOs = Get-ADOrganizationalUnit -Identity $OU | select -Expa...

PowerShell - Retrieve AD Computers Properties

# Retrieve AD computer properties such as LastLogonDate. # The list of machine need to be added in hosts.txt file in script folder # The result will be saved to Output.csv file in script folder Import-Module ActiveDirectory $invocation = (Get-Variable MyInvocation).Value $directorypath = Split-Path $invocation.MyCommand.Path $input = $directorypath + "\host.txt" $output = $directorypath + "\output.csv" Get-Content $input | ForEach-Object { Get-ADComputer $_ -Properties * | Select-Object Name,LastLogonDate,OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion,WhenCreated,Description }   | Export-Csv $output

PowerShell - Copy AD Group membership

#Copy group membership from source group to target group Import-Module ActiveDirectory $gsource =Get-ADGroup "Test Group1" $gtarget = Get-ADGroup "Test Group2" Get-ADGroupMember -Identity $gsource | foreach { Add-ADGroupMember -Identity $gtarget -Members $($_.DistinguishedName) }

Powershell : Executing a batch file on remote machines

Do you have a batch file which you want to run on multiple remote machines? The below powershell script can simplify this task. I have used this script to remotely repair SCCM client on multiple machines by executing batch files. The steps are quite simple Copy the below code to powershell script file. Provide list of machines in computers.txt file in same folder where you kept the script. Copy your batch file ‘MyFile.bat’  in same folder. Run the script.     Code: #Get current path $invocation = (Get-Variable MyInvocation).Value $directorypath = Split-Path $invocation.MyCommand.Path $Inputfile = $directorypath + "\computers.txt" $BatchFile = "c:\windows\temp\MyFile.bat" $source = $directorypath + "\Myfile.bat"   Get-Content $inputfile | ForEach-Object {     $computer = $_     $TargetPath = "\\$computer\admin$\temp\"     Write-Host  "==============================="       #Copy s...