Forum Discussion

Kerslock's avatar
Kerslock
Copper Contributor
Jul 21, 2022

Powershell Command for AD export

I have been searching for info, I am not even sure if what I am doing is feasible. All I want to do is export AD all computers. I have the command "Get-ADComputer -Filter "*" | Export-CSV -Path c:\Users\USERNAME\documents\Computers.csv" and this works fine, BUT... I want to be able to add a pull of serial numbers 1) either through the attribute editor in AD or 2) by using Get-WmiObject Win32_BIOS | Select SerialNumber and having it the serial number displayed in the .csv file.

 

I have tried so many combinations of commands, but nothing works. I even tried to add in the "ForEach" option.

 

I have used Get-WmiObject Win32_ComputerSystem | Select Manufacturer,Model to get get the info from my own computer, but if I am able to get it exported into the .csv as well - not required, but would be a bonus for my reports.

 

If I can get a command that combines basic AD computer properties & at least serial number, I would greatly appreciate it. And if the command can also include what to add in the get the make/model of the computer, I would appreciate it more! Thank you for your time and assistance!

  • farismalaeb's avatar
    farismalaeb
    Steel Contributor

    Kerslock 

    HI 

    Try this code

    $allPC=Get-ADComputer -Filter *
    $Fullresult=@()
    Foreach ($singlePC in $allPC){
    $Result=[PSCustomObject]@{
    Name=$singlePC.Name
    Enabled=$singlePC.Enabled
    BIOS=(Get-WmiObject Win32_BIOS -ComputerName $singlePC.name).SerialNumber 
    
    }
    $Fullresult+=$Result
    }
    $Fullresult | export-csv -path C:\Myresult.csv
      • farismalaeb's avatar
        farismalaeb
        Steel Contributor

        Kerslock 

        Use this

        $allPC=Get-ADComputer -Filter *
        $Fullresult=@()
        $UserNamePassword=Get-Credential
        Foreach ($singlePC in $allPC){
        $Result=[PSCustomObject]@{
        Name=$singlePC.Name
        Enabled=$singlePC.Enabled
        BIOS=(Get-WmiObject Win32_BIOS -ComputerName $singlePC.name -Credential $UserNamePassword -Authentication Default ).SerialNumber 
        
        }
        $Fullresult+=$Result
        }
        $Fullresult | export-csv -path C:\Myresult.csv

        In line 8, you can replace the authentication with anything that fits your env.

        The script asks for a username and password on line 3, just make sure to type it correct.

Resources