Pages

Showing posts with label Active Directory. Show all posts
Showing posts with label Active Directory. Show all posts

Tuesday, April 27, 2021

Export list of active computers from an OU to a .csv file

Get-ADComputer -filter {Enabled -eq $True} -Properties cn -SearchBase "OU=Production,DC=domain,DC=local" | select cn | ConvertTo-Csv -NoTypeInformation | Select-Object -skip 1 | Out-File <INSERT PATH TO .CSV FILE>

Thursday, July 9, 2015

Export list of computers from an OU to a .csv file

get-adcomputer -filter * -searchbase “OU=Test,DC=domain,DC=local" | select name | export-csv <INSERT PATH TO .CSV FILE>

Monday, July 6, 2015

Export AD group members to a .csv file

Get-ADGroupMember -identity "<INSERT GROUP NAME>" | select name | Export-csv -path <INSERT PATH TO .CSV FILE> -NoTypeInformation

Add list of users (by Display Name) to AD group from .csv file

This script will use a .csv file containing a list of users that need to be made members of an AD group. In this scenario only the display names are known, not the usernames.
The first cell in the column containing the user display names must use the value “Name”.

$ListOfUsers = Import-csv -Path <INSERT PATH TO .CSV FILE>
$ItemDetails = $NULL

foreach ($item in $ListOfUsers)
{
$a = $item.name
$ItemDetails = get-aduser -Filter {DisplayName -eq $a} -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
if (($ItemDetails.count) -gt 1)
{
Write-Host "Too many accounts with DisplayName $a" -ForegroundColor Yellow
}
else
{
if ($ItemDetails -eq $NULL)
    {
    Write-Host $a "does not exist in AD"  -ForegroundColor Red
    $ItemDetails = $NULL
    }
else
    {
    Write-Host $a  "does exist in AD" -ForegroundColor Green
    $ItemDetails.DistinguishedName
    $GroupToAddTo = get-adgroup -Identity "<INSERT GROUP NAME>"
    Add-ADGroupMember -Identity $GroupToAddTo -Member $ItemDetails.DistinguishedName
    $ItemDetails = $NULL
    }
}
}

Add list of users to AD group from .csv file

This script will use a .csv file containing a list of usernames that need to be made members of an AD group.
The first cell in the column containing the usernames must use the value “Name”.

$ListOfUsers = Import-csv -Path <INSERT PATH TO .CSV FILE>
$ItemDetails = $NULL

foreach ($item in $ListOfUsers)
{
$ItemDetails = get-aduser -Identity $item.name -properties * -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
if ($ItemDetails -eq $NULL)
    {
    Write-Host $item.name "does not exist in AD"  -ForegroundColor Red
    $ItemDetails = $NULL
    }
else
    {
    Write-Host $item.name  "does exist in AD" -ForegroundColor Green
    $ItemDetails.DistinguishedName
    $GroupToAddTo = get-adgroup -Identity "<INSERT GROUP NAME>"
    Add-ADGroupMember -Identity $GroupToAddTo -Member $ItemDetails.DistinguishedName
    $ItemDetails = $NULL
    }
}

Add list of computers to AD group from .csv file

This script will use a .csv file containing a list of computers that need to be made members of an AD group.
The first cell in the column containing the computer names must use the value “Name”.

$ListOfComputers = Import-csv –Path <INSERT PATH TO .CSV FILE>
foreach ($item in $ListOfComputers)
{
$ItemDetails = Get-ADComputer -identity $item.Name -properties * -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
if ($ItemDetails -eq $NULL)
{
Write-Host "$item does not exist in AD" -ForegroundColor Yellow
$ItemDetails = $NULL 
}
else
{
$ItemDetails.DistinguishedName
$GroupToAddTo = get-adgroup -Identity "<INSERT GROUP NAME>"
Add-ADGroupMember -Identity $GroupToAddTo -Member $ItemDetails.DistinguishedName   
}
}