This really applies to almost any PS command but I'm running:
Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase $strDomainDN -SearchScope OneLevel
and the output looks like:
$_.Name
------
OU1
OU2
OU3
开发者_JAVA百科...
And all I want is the actual list with not heading ($_.Name or the "------"). I know this is probably amazingly simple but also very frustrating when trying to format strings. Any help would be appreciated.
There are a bunch of ways to do this but one way is to pick off what you want with a foreach-object cmdlet. Assuming you want just the Name column you can do something like this:
13 > dir | select name | foreach-object {$_.Name} | out-string
Contacts
Desktop
Documents
Downloads
Favorites
Links
Lync Recordings
Music
NetApp
Pictures
Podcasts
PowerShellASP
Saved Games
Searches
Tracing
Videos
Virtual Machines
The Out-String forces it to output to a string instead of an array of strings for each line of the output.
PowerShell is what would happen if Perl and .Net reproduced.
This isn't an answer, but the reason Andy's answer works so well has everything to do with your unfamiliarity with this new tool. PowerShell "thinks" in objects. PowerShell's answer to the question, "Can you get me some organizational units?" is a handful of OU's. Those objects have methods and properties. Perl pipes strings and .NET doesn't "pipe" per se. PowerShell pipes objects.
The output you received is a CLI-formatted table of object properties. You asked PowerShell to return names, so it returned objects containing the property of Name. To represent those objects conveniently in a CLI/text way, PowerShell generates a table with a label, a separator "-----", and rows of data by calling their toString() method.
"Foreach" does the magic because it lets you control the formatting, but you'll want to become comfortable with this thing of objects being emitted by functions. Direct manipulation of "the object" itself, rather than a string representation of it, is what makes PowerShell so powerful.
I was having this same problem, but found a simpler solution.
(Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchScope OneLevel).Name
and in the general case, take any command you run and find the available properties, and then you can isolate any of them in the same way as '(...).Name' above, e.g. '(...).Country' or '(...).State'.
PS C:\temp\checkdirs> Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchScope OneLevel | gm
TypeName: Microsoft.ActiveDirectory.Management.ADOrganizationalUnit
Name MemberType Definition
---- ---------- ----------
Contains Method bool Contains(string propertyName)
Equals Method bool Equals(System.Object obj)
GetEnumerator Method System.Collections.IDictionaryEnumerator GetEnumerator()
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Item ParameterizedProperty Microsoft.ActiveDirectory.Management.ADPropertyValueCollection Item(s...
City Property System.String City {get;set;}
Country Property System.String Country {get;set;}
DistinguishedName Property System.String DistinguishedName {get;set;}
LinkedGroupPolicyObjects Property Microsoft.ActiveDirectory.Management.ADPropertyValueCollection Linked...
ManagedBy Property System.String ManagedBy {get;set;}
Name Property System.String Name {get;}
ObjectClass Property System.String ObjectClass {get;set;}
ObjectGUID Property System.Nullable`1[[System.Guid, mscorlib, Version=4.0.0.0, Culture=ne...
PostalCode Property System.String PostalCode {get;set;}
State Property System.String State {get;set;}
StreetAddress Property System.String StreetAddress {get;set;}
精彩评论