Skip to content

Instantly share code, notes, and snippets.

@lawndoc
Last active July 1, 2021 03:29
Show Gist options
  • Save lawndoc/c2446c0234184b2f427484b1072bfecd to your computer and use it in GitHub Desktop.
Save lawndoc/c2446c0234184b2f427484b1072bfecd to your computer and use it in GitHub Desktop.
Query AD for Windows Versions and Export to CSV
### Global Variables (edit these)
$DOMAIN = "DC=example,DC=com"
$CSVPATH = ".\\"
### Begin Script
$windowsXP = [System.Collections.ArrayList]@()
$windows7 = [System.Collections.ArrayList]@()
$windows10 = [System.Collections.ArrayList]@()
$server03 = [System.Collections.ArrayList]@()
$server08 = [System.Collections.ArrayList]@()
$server12 = [System.Collections.ArrayList]@()
$server16 = [System.Collections.ArrayList]@()
$server19 = [System.Collections.ArrayList]@()
$all_computers = Get-ADComputer -Filter * -SearchBase $DOMAIN -Properties OperatingSystem
foreach ($computer in $all_computers)
{
# ### Additional Checks (example below of additional exclusions)
# if ($computer.DistinguishedName -match "OU=Virtuals*")
# {
# continue
# }
switch -Wildcard ($computer.OperatingSystem)
{
"Windows XP *"
{
$windowsXP += $computer
}
"Windows 7 *"
{
$windows7 += $computer
}
"Windows 10 *"
{
$windows10 += $computer
}
"Windows Server 2003 *"
{
$server03 += $computer
}
"Windows Server 2008 *"
{
$server08 += $computer
}
"Windows Server 2012 *"
{
$server12 += $computer
}
"Windows Server 2016 *"
{
$server16 += $computer
}
"Windows Server 2019 *"
{
$server19 += $computer
}
}
}
$windowsXP | Export-Csv -Path "$($CSVpath)winXP.csv" -NoTypeInformation
$windows7 | Export-Csv -Path "$($CSVpath)win7.csv" -NoTypeInformation
$windows10 | Export-Csv -Path "$($CSVpath)win10.csv" -NoTypeInformation
$server03 | Export-Csv -Path "$($CSVpath)svr03.csv" -NoTypeInformation
$server08 | Export-Csv -Path "$($CSVpath)svr08.csv" -NoTypeInformation
$server12 | Export-Csv -Path "$($CSVpath)svr12.csv" -NoTypeInformation
$server16 | Export-Csv -Path "$($CSVpath)svr16.csv" -NoTypeInformation
$server19 | Export-Csv -Path "$($CSVpath)svr19.csv" -NoTypeInformation
### Global Variables (edit these)
$DOMAIN = "DC=example,DC=com"
$CSVPATH = ".\\"
$HOURSDELTA = "-48"
### Begin Script
$windowsXP = [System.Collections.ArrayList]@()
$windows7 = [System.Collections.ArrayList]@()
$windows10 = [System.Collections.ArrayList]@()
$server03 = [System.Collections.ArrayList]@()
$server08 = [System.Collections.ArrayList]@()
$server12 = [System.Collections.ArrayList]@()
$server16 = [System.Collections.ArrayList]@()
$server19 = [System.Collections.ArrayList]@()
$timeDelta = (Get-Date).AddHours($hoursDelta)
$all_computers = Get-ADComputer -Filter {whencreated -gt $timeDelta} -SearchBase $DOMAIN -Properties OperatingSystem
foreach ($computer in $all_computers)
{
# ### Additional Checks (example below of additional exclusions)
# if ($computer.DistinguishedName -match "OU=Virtuals*")
# {
# continue
# }
switch -Wildcard ($computer.OperatingSystem)
{
"Windows XP *"
{
$windowsXP += $computer
}
"Windows 7 *"
{
$windows7 += $computer
}
"Windows 10 *"
{
$windows10 += $computer
}
"Windows Server 2003 *"
{
$server03 += $computer
}
"Windows Server 2008 *"
{
$server08 += $computer
}
"Windows Server 2012 *"
{
$server12 += $computer
}
"Windows Server 2016 *"
{
$server16 += $computer
}
"Windows Server 2019 *"
{
$server19 += $computer
}
}
}
$windowsXP | Export-Csv -Path "$($CSVpath)winXP.csv" -NoTypeInformation
$windows7 | Export-Csv -Path "$($CSVpath)win7.csv" -NoTypeInformation
$windows10 | Export-Csv -Path "$($CSVpath)win10.csv" -NoTypeInformation
$server03 | Export-Csv -Path "$($CSVpath)svr03.csv" -NoTypeInformation
$server08 | Export-Csv -Path "$($CSVpath)svr08.csv" -NoTypeInformation
$server12 | Export-Csv -Path "$($CSVpath)svr12.csv" -NoTypeInformation
$server16 | Export-Csv -Path "$($CSVpath)svr16.csv" -NoTypeInformation
$server19 | Export-Csv -Path "$($CSVpath)svr19.csv" -NoTypeInformation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment