Help adding a value into an array

Hi all,
I am working on a script to help me move computer objects in AD from 1 OU to another.

I have the list of OU’s loaded into an array, but I want to add a number at the beginning of each output line with a number 1,2,3,4,etc… so that you can choose a number to pick what OU to move the computer to.

Here is what I have:

#MoveOU.ps1
$Computer = Read-Host -Prompt "What server do you want to move in AD?"
$AllOU = (Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=123,OU=xxxxxxxxxxx,OU=456,DC=xxxxx,DC=xyz' -SearchScope Subtree)
$OUArray = @($AllOU.DistinguishedName)
@OUCount = $OUArray.count
$OU = Read-Host -Prompt "What OU do you want to move it to?"

I have 149 line items from where I need to start looking. Can someone give pointers on how I can just insert a number for each line item at the beginning , like so:

1 - OU=123,OU=xxxxxxxxxxx,OU=456,DC=xxxxx,DC=xyz
2 - OU=123,OU=xxxxxxxxxxx,OU=456,DC=xxxxx,DC=xyz
3 - OU=123,OU=xxxxxxxxxxx,OU=456,DC=xxxxx,DC=xyz
etc…

I will then use this number as input to choose what OU to move the computer to.

Thanks for help,

-Matt

Matt,

a more “PowerShelly” way would be to use Out-GridView to choose the OU you want to use. Please read the help completely including the examples to learn how to use it.

If you insist to use an indexed list you can use a loop. You can use either

… or …

… or …

And there’s another new option if you want to be on the cutting edge of technology … :wink:

Olaf,
Thanks, Ill look into those fancier things, but for now

#MoveOU.ps1
$Computer = Read-Host -Prompt "What server do you want to move in AD?"
$AllOU = (Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=123,OU=xxxxxxxxxxx,OU=456,DC=xxxxx,DC=xyz' -SearchScope Subtree)
$OUArray = @($AllOU.DistinguishedName)
$OU = ($OUArray | Out-GridView -Title "Choose what OU to move object to" -OutputMode Single)
Get-ADComputer "$computer" | Move-ADObject -TargetPath $OU

I like that Out-GridView, good stuff!

-Matt