# Help with "if" "else" statement

**URL:** <https://forums.powershell.org/t/help-with-if-else-statement/19261>\
**Category:** PowerShell Help\
**Created:** [May 6, 2022, 6:46pm UTC](https://forums.powershell.org/t/help-with-if-else-statement/19261 "2022-05-06T18:46:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![shriram51](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@shriram51](https://forums.powershell.org/u/shriram51)\
**Post date:** [May 6, 2022, 6:46pm UTC](https://forums.powershell.org/t/help-with-if-else-statement/19261/1 "2022-05-06T18:46:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 6, 2022, 8:12pm UTC](https://forums.powershell.org/t/help-with-if-else-statement/19261/2 "2022-05-06T20:12:31Z")

</div>

Doodnauth,  
Welcome to the forum. 👋🏽

> [@shriram51](#):
>
> I would be happy to provide more details, if needed.

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

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.

---

<div class="post-metadata">

**Author:** ![shriram51](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@shriram51](https://forums.powershell.org/u/shriram51)\
**Post date:** [May 6, 2022, 8:27pm UTC](https://forums.powershell.org/t/help-with-if-else-statement/19261/3 "2022-05-06T20:27:06Z")

</div>

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](http://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 

```

}

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 6, 2022, 10:13pm UTC](https://forums.powershell.org/t/help-with-if-else-statement/19261/4 "2022-05-06T22:13:45Z")

</div>

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](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

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

```auto
$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

```

---

<div class="post-metadata">

**Author:** ![shriram51](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@shriram51](https://forums.powershell.org/u/shriram51)\
**Post date:** [May 7, 2022, 3:18am UTC](https://forums.powershell.org/t/help-with-if-else-statement/19261/5 "2022-05-07T03:18:02Z")

</div>

Hi Olaf,

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 7, 2022, 6:32am UTC](https://forums.powershell.org/t/help-with-if-else-statement/19261/6 "2022-05-07T06:32:14Z")

</div>

> [@shriram51](#):
>
> Thanks very much for your assistance.

You’re very welcome. 👍🏽

> [@shriram51](#):
>
> worked with a slight modification

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

---

<div class="post-metadata">

**Author:** ![shriram51](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@shriram51](https://forums.powershell.org/u/shriram51)\
**Post date:** [May 9, 2022, 4:03pm UTC](https://forums.powershell.org/t/help-with-if-else-statement/19261/7 "2022-05-09T16:03:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shriram51](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@shriram51](https://forums.powershell.org/u/shriram51)\
**Post date:** [May 10, 2022, 10:37pm UTC](https://forums.powershell.org/t/help-with-if-else-statement/19261/8 "2022-05-10T22:37:35Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 10, 2022, 11:54pm UTC](https://forums.powershell.org/t/help-with-if-else-statement/19261/9 "2022-05-10T23:54:14Z")

</div>

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

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.

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

```

and

```auto
$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`. 😉

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. 😉

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

---

<div class="post-metadata">

**Author:** ![shriram51](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@shriram51](https://forums.powershell.org/u/shriram51)\
**Post date:** [May 11, 2022, 3:34pm UTC](https://forums.powershell.org/t/help-with-if-else-statement/19261/10 "2022-05-11T15:34:39Z")

</div>

Hi Olaf,

Thanks for your suggestions. This is very helpful.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:17pm UTC](https://forums.powershell.org/t/help-with-if-else-statement/19261/11 "2024-05-16T20:17:56Z")

</div>


