Multi Copy

When you know scripting you also gain another extraordinary power which is laziness LOL. I am one of those lazy Admin’s who know Powershell and try to do everything using Powershell.

The Multi Copy script basically i got a set of files and folder location that need moving to different machines, servers and NAS. i was so lazy to move stuff that way (talking about 300 odd) so wrote the bad boy down.

$computers = get-content -path "machines name list" 
$source = read-host "Please type the Path and file name to Copy?" 
$destination = "destination location" 

foreach ($computer in $computers) { 

    if ((Test-Path -Path \\$computer\$destination)) { 
    Copy-Item $source -Destination \\$computer\$destination -Recurse 
    } else { 
    "\\$computer\$destination is not reachable or does not exist" 
    } 
} 

Moving User/s to an OU

This is quite fun, when i say fun in the system admin life 😛
When ever you move someone out of an OU, there is an 90% chance that they could get different set of Group Policy as OU and Group policy are together.

The script below will be help you in moving.

$data = get-content -Path "usernames that needs moving file"

foreach($username in $data) {

$targetOU = "Target OU "

    $display = Get-ADUser -Properties * -LDAPFilter "(displayName=$username)" | Select samaccountname,distinguishedName
    
    $name = $display.samaccountname
    $Path = $display.distinguishedName
        try{
            Move-ADObject -Identity $display.distinguishedName -TargetPath $targetOU
            Write-Host  $name + " - Moved" -ForegroundColor Green
        
        }catch{
       
            write-host
            write-host $name + " - Not Moved" -ForegroundColor Yellow
        }
      
}

Get Permission for an UNC Path

This specific requirement came to me when there was a file server migration.
having thousands of shared paths with different sets of permissions was an issue before we moved into DFS and this little script saved my time.

$path = Read-host “Enter a UNC Path: ”

write-host
"File Sharing Permissions Report - $path"
Write-Host
 
$acl = Get-Acl $path
 
Write-Host "File/NTFS Permissions"
Write-Host
 
foreach($accessRule in $acl.Access)
{
    Write-Host " * " $accessRule.IdentityReference $accessRule.FileSystemRights
}

Export all users in AD

This could be very helpful for reports, where management loves reports 😛

$users = @()
$pathtosave1 = "Path to save.csv"
Add-Content -Path $pathtosave1 -Value '"Fist Name","Last Name","SAM Name","Email Address","Enabled"'

$users += Get-ADUser -Filter * -SearchBase "give the base of the Domain" -Properties * | select givenname,surname,samaccountname,mail,enabled

foreach($user in $users)
{
 Add-Content -Path $pathtosave1 -value ($user.givenname+","+$user.surname+","+$user.samaccountname+","+$user.mail+","+$user.enabled)
} 

Get All users in a specific OU

This comes quite handy in an organization where OU structure is properly maintained
or even in a messy organization if you are trying to sort it.

$ou = "OU you want to get the users from"
$arr = @()
$pathtosave = "path to save.csv"

Add-Content -Path $pathtosave -Value '"First Name","Last Name","SAM Name","Description","OU"'

$arr = get-aduser -SearchBase $ou -Properties * -Filter * | select givenname,surname,samaccountname,Description,DistinguishedName,Enabled  |? {$_.enabled -eq $true}

foreach($unit in $arr){
Add-Content -Path $pathtosave -value ($unit.givenname+","+$unit.surname+","+$unit.samaccountname+","+$unit.Description+","+$unit.DistinguishedName)
}

Get All Users Excluding an OU

Getting all the user in AD is quite easy but excluding an OU could be bit of a challenging.

The below script will be able to help you achieve you this.

$OUDN = "Organization Unit Distinguished name that you want to exclude"
$arr1 = @()

$pathtosave = "Save location .csv"

Add-Content -Path $pathtosave -Value '"Fist Name","Last Name","SAM Name","Description","OU"'


$arr1 += Get-ADUser -Filter {Enabled -eq $true} | select givenname,surname,samaccountname,Description,DistinguishedName | Where-Object { $_.DistinguishedName -notlike "*,$OUDN" }

foreach($unit in $arr1){
Add-Content -Path $pathtosave -value ($unit.givenname+","+$unit.surname+","+$unit.samaccountname+","+$unit.Description+","+$unit.DistinguishedName)
}

Get Machines Name starting/ending with

This Scenario is mostly common when it comes to quick reports on Datacenter,department,floor,team etc.. Most of the organizations have naming convention to identify machine by the name with a specific set of letter in the name

For instance: MI-PDC-L2-R1-MGT01 this is a server name. To make it clear let me breakdown
MI = Melbourne
PDC = Primary Data Center
L2 = Level 2
R1 = Rack 1
MGT01 = Management Server 01

You can use the below script to find the machines that are in any of the above criteria

Get-ADComputer -Filter * | Where-Object {$_.Name -like “*machine name *”} | Select -Property Name,enabled 

To export it as a report

Get-ADComputer -Filter * | Where-Object {$_.Name -like "*machine name *"} | Select -Property Name,enabled >> "Output path.txt"

Check if a user/s is enabled or disable

this task is very straight forward. The script below achieves this.

Import-Module activedirectory

$reader = [System.IO.File]::OpenText("parse user list.txt")

while($null -ne ($user= $reader.ReadLine())) {

$checkeduser= Get-ADUser -Identity $user
write-host
"------------------------"
"User " + $checkeduser.SamAccountName + " is Enabled : " + $checkeduser.Enabled
"OU Path is : " + $checkeduser.distinguishedname
"------------------------"
}

Check a Machine is installed with an Update

Everyone loves quick wins,

The script below will tell you if a machine or more than one is being installed with a specific update.

 Get-Hotfix -computername 'Machine name'| where {$_.HotfixID -eq "KB######"} | Select-Object HotfixID

Check if a device is up through IP address

Checking if a machine is up can be a task similar to eating a cake. Only if other teams decide to send you a list of IP address and ask you to check if the IP is pingable. The script below will come hand for this purpose.

$machines = 'IP Addresses'

foreach ($machine in $machines) { 
        if (test-Connection -ComputerName $machine -Count 2 -Quiet ) {  
         
            write-Host "$machine is alive and Pinging " -ForegroundColor Green 
                     
                    } else 
                     
                    { Write-Warning "$machine seems dead not pinging" 
             
                    }     
}