Get attribute from list of OUs

The code below is an attempt to return an attribute from a list of OUs and write the results to csv.  I've used this script for user and computer objects, but the results I get with this script leave System.Object in the associated csv fields.  Anyone know what I'm doing wrong?  The list.txt file could contain either the full OU DN, or simply a list of OUs... both return the same result.

Get-Content c:\temp\list.txt | ForEach {

    $OUProps = Get-ADOrganizationalUnit -filter * -properties st | Select-Object st

    New-Object psobject -Property @{Name=$OUProps.Name;st=$OUProps.st}

} | Export-Csv c:\temp\PS_getuserattribute\output.csv -NoTypeInformation

August 31st, 2015 10:38am

"st" is not a property of this cmdlet. If you are looking for State or StreetAddress then you need to use either "state" or StreetAddress" in repalce of the st in the $OUProps variable.

Once you have done that you should get your results.

You can also verify this using the following command...

Get-ADOrganizationalUnit -Identity "ou=testou,dc=example,dc=com" -Properties * | get-member

This will show you all of the available properties you can use.

You were able to use st in user and computer because ST is a property of those cmdlets.

Will.


Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 10:43am

$OUProps will only have an st property after the assignment (because of the "Select-Object st" at the end), so it will not have a Name property specified on the next line. Also, at no point do you use the element from list.txt (no reference of $_ or $PSItem within the loop) so you're just getting all OUs every time through the loop.
August 31st, 2015 10:49am

"st" is not a property of this cmdlet. If you are looking for State or StreetAddress then you need to use either "state" or StreetAddress" in repalce of the st in the $OUProps variable.

Once you have done that you should get your results.

You can also verify this using the following command...

Get-ADOrganizationalUnit -Identity "ou=testou,dc=example,dc=com" -Properties * | get-member

This will show you all of the available properties you can use.

You were able to use st in user and computer because ST is a property of those cmdlets.

Will.


Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 10:51am

$OUProps will only have an st property after the assignment (because of the "Select-Object st" at the end), so it will not have a Name property specified on the next line. Also, at no point do you use the element from list.txt (no reference of $_ or $PSItem within the loop) so you're just getting all OUs every time through the loop.
August 31st, 2015 10:57am

Lots of explanations but no results:

Get-Content c:\temp\list.txt | 
    ForEach {
        Get-ADOrganizationalUnit $_ | Select-Object Name,st
    } | 
    Export-Csv c:\temp\PS_getuserattribute\output.csv -NoTypeInformation

List.txt should contain samaccountnames or DNs.

Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 12:31pm

Lots of explanations but no results:

Get-Content c:\temp\list.txt | 
    ForEach {
        Get-ADOrganizationalUnit $_ | Select-Object Name,st
    } | 
    Export-Csv c:\temp\PS_getuserattribute\output.csv -NoTypeInformation

List.txt should contain samaccountnames or DNs.

August 31st, 2015 12:36pm

Lots of explanations but no results:

Get-Content c:\temp\list.txt | 
    ForEach {
        Get-ADOrganizationalUnit $_ | Select-Object Name,st
    } | 
    Export-Csv c:\temp\PS_getuserattribute\output.csv -NoTypeInformation

List.txt should contain samaccountnames or DNs.

Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 12:44pm

st is not a property exposed by the PowerShell cmdlet. It is an attribute of the AD object. As such, you will only retrieve a value if you specify the attribute with the -Properties parameter. However, the cmdlet does expose the State property, which is the same thing. It is even a default property, so it is always retrieved.

jrv is correct, you need to use $_ to retrive the OU specified in the text file. But OU objects do not have sAMAccountName. OU's must be identified by either distinguishedName or GUID. The Name (Relative Distinguished Name) will not uniquely identify the object, so you need the full distinguished name. I would suggest:

Get-Content c:\temp\list.txt | 
    ForEach {
        Get-ADOrganizationalUnit $_ | Select-Object Name,state
    } | 
    Export-Csv c:\temp\PS_getuserattribute\output.csv -NoTypeInformation

where list.txt is a list of distinguished names of OU's.
August 31st, 2015 12:58pm

Thanks for the responses - looks like I'm doing quite a bit wrong. Here's where I stand, and since I don't have a full list of DNs for the OUs I'm trying to get results on - I'm trying to simply use the OU name in list.txt.

Get-Content c:\list.txt | 
    ForEach {
        Get-ADOrganizationalUnit $_ -Filter * -SearchBase "OU=xx,DC=xx,DC=xx,DC=xx,DC=xx" -SearchScope Subtree | Select-Object Name,state
    } | 
    Export-Csv c:\temp\output.csv -NoTypeInformation
With the &_ element in there I get a positional parameter error, but without it I get results from all OUs under the one specified in the script.

Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 1:01pm

Thanks for the responses - looks like I'm doing quite a bit wrong. Here's where I stand, and since I don't have a full list of DNs for the OUs I'm trying to get results on - I'm trying to simply use the OU name in list.txt.

Get-Content c:\list.txt | 
    ForEach {
        Get-ADOrganizationalUnit $_ -Filter * -SearchBase "OU=xx,DC=xx,DC=xx,DC=xx,DC=xx" -SearchScope Subtree | Select-Object Name,state
    } | 
    Export-Csv c:\temp\output.csv -NoTypeInformation
With the &_ element in there I get a positional parameter error, but without it I get results from all OUs under the one specified in the script.

I assume you mean "$_", not "&_". Yes, it doesn't know what you mean by just putting the "$_" there; you already have "-Filter *", so it looks like you're trying to use $_ as an additional filter but there are no positional parameters that fit so the cmdlet complains about it.

Regardless, I think what you'll need to do, since you say you only have the names of OUs, not DNs or GUIDs, is to use something like this to find the OUs you're looking for (note: not tested code!):

Get-ADOrganizationalUnit -Filter * -SearchBase "OU=xx,DC=xx,DC=xx,DC=xx" -SearchScope Subtree | ? { $_.DistinguishedName -match "^OU=$($_),.*" } | Select-Object Name,State
This uses the Where-Object cmdlet to filter the list of OUs returned to ones that match the names in the text file. Note, however, that since OU names are not unique you may end up with more than one that matches a single input name.


August 31st, 2015 1:14pm

Thanks for the responses - looks like I'm doing quite a bit wrong. Here's where I stand, and since I don't have a full list of DNs for the OUs I'm trying to get results on - I'm trying to simply use the OU name in list.txt.

Get-Content c:\list.txt | 
    ForEach {
        Get-ADOrganizationalUnit $_ -Filter * -SearchBase "OU=xx,DC=xx,DC=xx,DC=xx,DC=xx" -SearchScope Subtree | Select-Object Name,state
    } | 
    Export-Csv c:\temp\output.csv -NoTypeInformation
With the &_ element in there I get a positional parameter error, but without it I get results from all OUs under the one specified in the script.

I assume you mean "$_", not "&_". Yes, it doesn't know what you mean by just putting the "$_" there; you already have "-Filter *", so it looks like you're trying to use $_ as an additional filter but there are no positional parameters that fit so the cmdlet complains about it.

Regardless, I think what you'll need to do, since you say you only have the names of OUs, not DNs or GUIDs, is to use something like this to find the OUs you're looking for (note: not tested code!):

Get-ADOrganizationalUnit -Filter * -SearchBase "OU=xx,DC=xx,DC=xx,DC=xx" -SearchScope Subtree | ? { $_.DistinguishedName -match "^OU=$($_),.*" } | Select-Object Name,State
This uses the Where-Object cmdlet to filter the list of OUs returned to ones that match the names in the text file. Note, however, that since OU names are not unique you may end up with more than one that matches a single input name.


Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 1:22pm

Yes, $_ sorry.

Tried the above and though no errors I get blank results in the csv.

Appreciate the attempt.

August 31st, 2015 1:57pm

Yes, $_ sorry.

Tried the above and though no errors I get blank results in the csv.

Appreciate the attempt.

If you are using a distinguished name or a samaccountname then this is all you need:

Get-Content c:\list.txt | 
    ForEach {
        Get-ADOrganizationalUnit $_     
    } | 
    Select-Object Name, st |
    Export-Csv c:\temp\output.csv -NoTypeInformation

If you still get nothing then try testing some of the llines in the file:

Get-Content c:\list.txt |
    ForEach {  Get-ADOrganizationalUnit $_   }

If this returns nothing then you have bad data inm your file or a bad file.

Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 2:30pm

"st" is not a property of this cmdlet. If you are looking for State or StreetAddress then you need to use either "state" or StreetAddress" in repalce of the st in the $OUProps variable.

Once you have done that you should get your results.

You can also verify this using the following command...

Get-ADOrganizationalUnit -Identity "ou=testou,dc=example,dc=com" -Properties * | get-member

This will show you all of the available properties you can use.

You were able to use st in user and computer because ST is a property of those cmdlets.

Will.


August 31st, 2015 2:41pm

$OUProps will only have an st property after the assignment (because of the "Select-Object st" at the end), so it will not have a Name property specified on the next line. Also, at no point do you use the element from list.txt (no reference of $_ or $PSItem within the loop) so you're just getting all OUs every time through the loop.
  • Edited by D'Thompson Monday, August 31, 2015 2:48 PM
Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 2:47pm

Thanks for the responses - looks like I'm doing quite a bit wrong. Here's where I stand, and since I don't have a full list of DNs for the OUs I'm trying to get results on - I'm trying to simply use the OU name in list.txt.

Get-Content c:\list.txt | 
    ForEach {
        Get-ADOrganizationalUnit $_ -Filter * -SearchBase "OU=xx,DC=xx,DC=xx,DC=xx,DC=xx" -SearchScope Subtree | Select-Object Name,state
    } | 
    Export-Csv c:\temp\output.csv -NoTypeInformation
With the &_ element in there I get a positional parameter error, but without it I get results from all OUs under the one specified in the script.

I assume you mean "$_", not "&_". Yes, it doesn't know what you mean by just putting the "$_" there; you already have "-Filter *", so it looks like you're trying to use $_ as an additional filter but there are no positional parameters that fit so the cmdlet complains about it.

Regardless, I think what you'll need to do, since you say you only have the names of OUs, not DNs or GUIDs, is to use something like this to find the OUs you're looking for (note: not tested code!):

Get-ADOrganizationalUnit -Filter * -SearchBase "OU=xx,DC=xx,DC=xx,DC=xx" -SearchScope Subtree | ? { $_.DistinguishedName -match "^OU=$($_),.*" } | Select-Object Name,State
This uses the Where-Object cmdlet to filter the list of OUs returned to ones that match the names in the text file. Note, however, that since OU names are not unique you may end up with more than one that matches a single input name.


  • Edited by D'Thompson Monday, August 31, 2015 5:15 PM
August 31st, 2015 5:12pm

As i stated in my first post ST is not a valid property which is why it is not working for you, with that cmdlet.

Get-Content c:\temp\list.txt | ForEach {

Get-ADOrganizationalUnit $_ | 
Select Name, DistinguishedName, State |
Export-Csv "c:\temp\PS_getuserattribute\output.csv" -NoTypeInformation -Append

}

That should do it.

Will.

Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 7:33pm

As i stated in my first post ST is not a valid property which is why it is not working for you, with that cmdlet.

Get-Content c:\temp\list.txt | ForEach {

Get-ADOrganizationalUnit $_ | 
Select Name, DistinguishedName, State |
Export-Csv "c:\temp\PS_getuserattribute\output.csv" -NoTypeInformation -Append

}

That should do it.

Will.

Yep I've been using 'state' since your last post, however I'm not able to get results likely due to only having the OU names in the txt file list.  Is the DN an absolute requirement?  Any way around this?  Here's the error:

Get-ADOrganizationalUnit : Cannot find an object with identity: 'OUName' under: 'DC=...

FullyQualifiedErrorId : Cannot find an object with identity: 'OUName' ...

August 31st, 2015 7:52pm

The Get-ADOrganizationalUnit cmdlet requires either the full distinguishedName or the GUID. Only these values will uniquely identify the object in AD.

If you only have RDN's in the text file, and you can be sure that the names are unique among OU's (there can be duplicates among objects like groups), then you can use a filter to identify the OU. The below is not tested.

Get-ADOrganizationalUnit -Filter {Name -eq $($_)}

In general, this could retrieve more than one OU, but if you know the names are unique in your environment, then something like this will work for you.

Also, if you do not use the -Properties parameter, then you must refer to the State property, which is a default property that is always retrieved by the cmdlet. The st attribute will only be retrieved if requested with the -Properties parameter.

Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 8:55pm

st and state are identical.  If you need ST by name then follow Richard's directions. II recommend using 'State'.

Get-ADOrganizationalUnit  -filter "name -eq 'testou3'" |select state

Get-ADOrganizationalUnit  -filter "name -eq 'testou3'" -properties st |select st

It is a name thing.

August 31st, 2015 10:38pm

Get-Content c:\temp\list.txt |
    ForEach {
Get-ADOrganizationalUnit -Filter "Name -eq '$($_)'" -SearchBase "OU=xxx,OU=xxx,DC=xxx,DC=xxx,DC=xxx,DC=xxx" -SearchScope Subtree -properties state
    } | 
    Export-Csv c:\temp\output.csv -NoTypeInformation

I was able to get the info needed from this script, it returns City, Country, DN, Linked GPOs, Managed By, Name, ObjectClass, Object GUID, Postal Code, State and Street Address.  The list of OU names only were in the txt file.

Thanks to everyone who chimed in..

Free Windows Admin Tool Kit Click here and download it now
September 1st, 2015 9:05am

Get-Content c:\temp\list.txt |
    ForEach {
Get-ADOrganizationalUnit -Filter "Name -eq '$($_)'" -SearchBase "OU=xxx,OU=xxx,DC=xxx,DC=xxx,DC=xxx,DC=xxx" -SearchScope Subtree -properties state
    } | 
    Export-Csv c:\temp\output.csv -NoTypeInformation

I was able to get the info needed from this script, it returns City, Country, DN, Linked GPOs, Managed By, Name, ObjectClass, Object GUID, Postal Code, State and Street Address.  The list of OU names only were in the txt file.

Thanks to everyone who chimed in..

  • Proposed as answer by David das Neves Tuesday, September 01, 2015 1:04 PM
September 1st, 2015 1:03pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics