Help with "if" "else" statement

Appreciate your assistance with a script I am working on. The script extracts all computers from an OU and a file with two columns: Computer Name and ExtensionAttribute.
I would also like to filter the list with an “if” “else” statement. If the value is X, move the computers to OU1. Else move the computers to OU2.
I would be happy to provide more details, if needed.
Thanks for your assistance.

Doodnauth,
Welcome to the forum. :wave:t4:

You should provide your script and an explanation what’s not working or what you’ve got stuck with. :wink:

Please keep in mind … This forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request.

We expect you to make an own attempt first to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you step further.

Hi Olaf,
Thanks for your response. This is my script. It fails with message “the term ‘else’ is not recognized as the name of a cmdlet, function, script file, or executable program”.

$TempOU = “OU=X,DC=Y,DC=com”

Specify path to the text file with the computer account names.

$ExportPath= “C:\Scripts\TempOUComputers.csv”

Specify path to the OU where computers will be moved.

$TargetOUS1 = “OU=A,DC=Y,DC=com”
$TargetOUS2 = “Ou=B,DC=Y,DC=com”

Get-ADComputer -Filter * -SearchBase $TempOU -Properties * | Select-Object CN,ExtensionAttribute1 | Export-Csv -NoTypeInformation $ExportPath

Move Computers to the new OU

$MoveList = Import-csv -Path $ExportPath
{Foreach ($Computer in $MoveList) {
Get-ADComputer $Computer.CN, $Computer.ExtensionAttribute1}
if ($Computer.ExtensionAttribute1 = “1”) {Move-ADObject -TargetPath $TargetOU1} }
else {Move-ADObject -TargetPath $TargetOUS2} {

Write-Output "Computer account $computer.CN has been moved successfully $(Get-Date)" >> C:\Scripts\Result.txt 

}

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

I cannot test at the moment but something like this hsould be enough actually:

$TempOU = 'OU=X,DC=Y,DC=com'
$TargetOU1 = 'OU=A,DC=Y,DC=com'
$TargetOU2 = 'Ou=B,DC=Y,DC=com'
$ExportPath = 'C:\Scripts\TempOUComputers.csv'
$PropertyList = 'CN', 'ExtensionAttribute1'

$ADComputerList = 
    Get-ADComputer -Filter * -SearchBase $TempOU -Properties $PropertyList

$ADComputerList | 
    Where-Object -Property ExtensionAttribute1 -EQ -Value 'X' |
        Move-ADObject -TargetPath $TargetOU1
$ADComputerList | 
    Where-Object -Property ExtensionAttribute1 -NE -Value 'X' |
        Move-ADObject -TargetPath $TargetOU2

$ADComputerList | 
    Select-Object -Property $PropertyList |
        Export-Csv -Path $ExportPath -NoTypeInformation

Hi Olaf,

Thanks very much for your assistance. The script worked with a slight modification. I really appreciate your help.

You’re very welcome. :+1:t4:

I’m, curious. What did you change and why? :wink:

Hi Olaf,
Your script was perfect. I only had to add a line to output the result to a text file. Sorry for any confusion, and thanks again.

Hi Olaf,
Grateful for your assistance one more time. The results in my output text file does not actually populate the computer names that have been moved. I would like to loop through the computer names from the .csv file and show them in a result.txt file. Appreciate if you can help me to modify this line:

foreach ($Computer in $ExportPath)   
{
Write-Output "Computer '$Computer.CN' has been moved successfully $(Get-Date)" >> C:\Scripts\Result.txt 
}

Actually you already have the needed information in the list you created with the initial query. :wink:

But if you insist to have it in separate files … assumed the move of the computer account objects always works as desired I’d use the same query used to initiate the move for outputting the accounts to a log file if needed.

$ADComputerList | 
    Where-Object -Property ExtensionAttribute1 -EQ -Value 'X' |
        Export-Csv -Path 'ComputerMovedToTargetOU01.csv' -NoTypeInformation

and

$ADComputerList | 
    Where-Object -Property ExtensionAttribute1 -NE -Value 'X' |
        Export-Csv -Path 'ComputerMovedToTargetOU02.csv' -NoTypeInformation

Another option would be to add the parameter -PassThru to your move commands to advice the cmdlet to output an object representing the moved object. This output could be captured either with the common parameter -OutVariable or with an explicit variable assignment in front of the command. Or You could export it directly to a CSV file with Export-Csv. :wink:

To have a plain text file with a lot of lines containing prose like “Computer 'COMPUTER01' has been moved successfully 2022-05-11” is the worst idea I can think of. That would make much harder than necessary to read the file with PowerShell and process the contained data. :wink:

If I needed a log file it would probably be a CSV file with the headers ComputerName, MovedFrom, MovedTo, Date. :wink:

Hi Olaf,

Thanks for your suggestions. This is very helpful.