<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>My Website &#187; PropertyValueCollection</title>
	<atom:link href="http://jansveld.net/powershell/?feed=rss2&#038;tag=propertyvaluecollection" rel="self" type="application/rss+xml" />
	<link>http://jansveld.net/powershell</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Wed, 09 Oct 2013 18:31:27 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.6.1</generator>
		<item>
		<title>Select-Properties: a helper for exporting DirectoryEntry objects</title>
		<link>http://jansveld.net/powershell/?p=7</link>
		<comments>http://jansveld.net/powershell/?p=7#comments</comments>
		<pubDate>Mon, 02 Jun 2008 02:44:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[DirectoryEntry]]></category>
		<category><![CDATA[export]]></category>
		<category><![CDATA[PropertyValueCollection]]></category>

		<guid isPermaLink="false">http://www.jansveld.net/powershell/?p=4</guid>
		<description><![CDATA[When you work with DirectoryEntry objects (AD or local accounts), you quickly run into the fact that their properties are actually PropertyValueCollections. This trips you up with certain operations, like exporting entries. For instance, the following will not work: # Get some user accounts $Users = ([ADSI]&#8216;WinNT://.&#8216;).PSBase.Children &#124; where {$_.PSBase.SchemaClassName -eq &#8216;User&#8216;} # Export them [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>When you work with DirectoryEntry objects (AD or local accounts), you quickly run into the fact that their properties are actually PropertyValueCollections. This trips you up with certain operations, like exporting entries. For instance, the following will not work:</p>
<p><span style="color: #008000;">#</span><span style="color: #008000;"> Get some user accounts</span><span style="color: #008000;"><br />
</span><span style="color: #800080;">$Users</span><span style="color: #000000;"> </span><span style="color: #ff0000;">=</span><span style="color: #000000;"> ([</span><span style="color: #008080;">ADSI</span><span style="color: #000000;">]</span><span style="color: #800000;">&#8216;</span><span style="color: #800000;">WinNT://.</span><span style="color: #800000;">&#8216;</span><span style="color: #000000;">).PSBase.Children | </span><span style="color: #0000ff;">where</span><span style="color: #000000;"> {</span><span style="color: #000080;">$_</span><span style="color: #000000;">.PSBase.SchemaClassName </span><span style="color: #ff0000;">-eq</span><span style="color: #000000;"> </span><span style="color: #800000;">&#8216;</span><span style="color: #800000;">User</span><span style="color: #800000;">&#8216;</span><span style="color: #000000;">}<br />
</span><span style="color: #008000;">#</span><span style="color: #008000;"> Export them to a CSV file</span><span style="color: #008000;"><br />
</span><span style="color: #800080;">$Users</span><span style="color: #000000;"> | </span><span style="font-weight: bold; color: #5f9ea0;">Export-Csv</span><span style="color: #000000;"> </span><span style="color: #800000;">Users</span><span style="color: #800000;">.csv</span><span style="color: #000000;"><br />
</span><br />
There are no errors, but the exported file will be filled with the text &#8220;System.DirectoryServices.PropertyValueCollection&#8221;. Not really what we were looking for. The solution is to let PowerShell turn those PropertyValueCollections into regular values. If we just want to get the names and descriptions, we can use:</p>
<p><span style="color: #800080;">$Users</span><span style="color: #000000;"> | </span><span style="font-weight: bold; color: #5f9ea0;">select</span><span style="color: #000000;"> {</span><span style="color: #000080;">$_</span><span style="color: #000000;">.Name}, {</span><span style="color: #000080;">$_</span><span style="color: #000000;">.Description} | </span><span style="font-weight: bold; color: #5f9ea0;">Export-Csv</span><span style="color: #000000;"> </span><span style="color: #800000;">Users.csv</span></p>
<p>That&#8217;s good enough when you are in a hurry. But if you want the actual property names, and need many (or all) properties, this calls for a scripted solution. This is the version I came up with originally:</p>
<pre><span style="color: #0000ff;">filter</span><span style="color: #000000;"> </span><span style="color: #5f9ea0;">Select-Properties</span><span style="color: #000000;"> (</span><span style="color: #800080;">$filter</span><span style="color: #ff0000;">=</span><span style="color: #800000;">'</span><span style="color: #800000;">*</span><span style="color: #800000;">'</span><span style="color: #000000;">) {
    </span><span style="color: #008000;">#</span><span style="color: #008000;"> Create a custom object to hold the requested properties</span><span style="color: #008000;">
</span><span style="color: #000000;">    </span><span style="color: #800080;">$custom</span><span style="color: #000000;"> </span><span style="color: #ff0000;">=</span><span style="color: #000000;"> </span><span style="font-weight: bold; color: #5f9ea0;">New-Object</span><span style="color: #000000;"> </span><span style="color: #800000;">PSObject</span><span style="color: #000000;">
    </span><span style="color: #008000;">#</span><span style="color: #008000;"> Get the properties of the object being processed</span><span style="color: #008000;">
</span><span style="color: #000000;">    </span><span style="color: #800080;">$props</span><span style="color: #000000;"> </span><span style="color: #ff0000;">=</span><span style="color: #000000;"> </span><span style="color: #000080;">$_</span><span style="color: #000000;">.PSBase.Properties
    </span><span style="color: #008000;">#</span><span style="color: #008000;"> Add each property that matches our filter to the custom object</span><span style="color: #008000;">
</span><span style="color: #000000;">    </span><span style="color: #800080;">$filter</span><span style="color: #000000;"> | </span><span style="font-weight: bold; color: #5f9ea0;">foreach</span><span style="color: #000000;"> {
        </span><span style="color: #800080;">$props</span><span style="color: #000000;">.PropertyNames </span><span style="color: #ff0000;">-like</span><span style="color: #000000;"> </span><span style="color: #000080;">$_</span><span style="color: #000000;"> | </span><span style="font-weight: bold; color: #5f9ea0;">foreach</span><span style="color: #000000;"> {
            </span><span style="color: #800080;">$custom</span><span style="color: #000000;"> | </span><span style="font-weight: bold; color: #5f9ea0;">add-Member</span><span style="color: #000000;"> </span><span style="color: #800000;">noteProperty</span><span style="color: #000000;"> </span><span style="color: #000080;">$_</span><span style="color: #000000;"> $(</span><span style="color: #800080;">$props</span><span style="color: #000000;">.Item(</span><span style="color: #000080;">$_</span><span style="color: #000000;">))
        }
    }
    </span><span style="color: #008000;">#</span><span style="color: #008000;"> Return the custom object</span><span style="color: #008000;">
</span><span style="color: #000000;">    </span><span style="color: #800080;">$custom</span><span style="color: #000000;">
}
</span></pre>
<p>Now you can use</p>
<p><span style="color: #800080;">$Users</span><span style="color: #000000;"> | </span><span style="color: #5f9ea0;">Select-Properties</span><span style="color: #000000;"> | </span><span style="font-weight: bold; color: #5f9ea0;">Export-Csv</span><span style="color: #000000;"> </span><span style="color: #800000;">Users.csv</span></p>
<p><span style="color: #800000;"><span style="color: #000000;">and get the data you really wanted. You can also specify one or more specific properties (with wildcards), just like the Select-Object command. The script has a few drawbacks, but we will save that (and an improved script) for a future post.</span></span></p>
<p>Arnoud</p>
<p> </p>
]]></content:encoded>
			<wfw:commentRss>http://jansveld.net/powershell/?feed=rss2&#038;p=7</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
