# Writing properties of an object to file

**URL:** <https://forums.powershell.org/t/writing-properties-of-an-object-to-file/7067>\
**Category:** PowerShell Help\
**Created:** [August 25, 2016, 8:11am UTC](https://forums.powershell.org/t/writing-properties-of-an-object-to-file/7067 "2016-08-25T08:11:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![aaron-miller](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@aaron-miller](https://forums.powershell.org/u/aaron-miller)\
**Post date:** [August 25, 2016, 8:11am UTC](https://forums.powershell.org/t/writing-properties-of-an-object-to-file/7067/1 "2016-08-25T08:11:16Z")

</div>

How can I pull, say, the name property to file from ‘search-adaccount’ as it appears on the object?

Instead of writing ‘Aaron’ it will write out like cn=‘Aaron’,cn=‘users’,cn=‘whatever…’

I tried just doing a split/replace but it yields the same results…

```
Add-Content out.txt (Search-ADAccount -LockedOut) | % {$($_.name.split(",")[0] -replace "cn=","")}
```

---

<div class="post-metadata">

**Author:** ![aaron-miller](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@aaron-miller](https://forums.powershell.org/u/aaron-miller)\
**Post date:** [August 25, 2016, 8:28am UTC](https://forums.powershell.org/t/writing-properties-of-an-object-to-file/7067/2 "2016-08-25T08:28:04Z")

</div>

I’m trying to add 2 properties to a listbox using:

```
function Load-ListBox 
	{
	
		Param (
			[ValidateNotNull()]
			[Parameter(Mandatory=$true)]
			[System.Windows.Forms.ListBox]$ListBox,
			[ValidateNotNull()]
			[Parameter(Mandatory=$true)]
			$Items,
		    [Parameter(Mandatory=$false)]
			[string]$DisplayMember,
			[switch]$Append
		)
		
		if(-not $Append)
		{
			$listBox.Items.Clear()	
		}
		
		if($Items -is [System.Windows.Forms.ListBox+ObjectCollection] -or $Items -is [System.Collections.ICollection])
		{
			$listBox.Items.AddRange($Items)
		}
		elseif ($Items -is [System.Collections.IEnumerable])
		{
			$listBox.BeginUpdate()
			foreach($obj in $Items)
			{
				$listBox.Items.Add($obj)
			}
			$listBox.EndUpdate()
		}
		else
		{
			$listBox.Items.Add($Items)	
		}
	
		$listBox.DisplayMember = $DisplayMember	
	}

$xyz = (Search-ADAccount -LockedOut) | % {"$($_.name), $($_.samaccountname)"}
Load-ListBox $LockedList $xyz
```

its loading the objects in the list box as

cn=aaron, cn=users, cn=amunson, cn=users

when i want it to look like…

Aaron, amunson

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [August 25, 2016, 8:28am UTC](https://forums.powershell.org/t/writing-properties-of-an-object-to-file/7067/3 "2016-08-25T08:28:46Z")

</div>

Use the name? The distinguishename is cn=… why are you trying to split the name property for characters it doesn’t include?

(Search-ADAccount -LockedOut | select -First 5).name |tee lockedout.txt

---

<div class="post-metadata">

**Author:** ![wilfredo-perez](https://avatars.discourse-cdn.com/v4/letter/w/e99b99/32.png) [@wilfredo-perez](https://forums.powershell.org/u/wilfredo-perez)\
**Post date:** [August 25, 2016, 8:36am UTC](https://forums.powershell.org/t/writing-properties-of-an-object-to-file/7067/4 "2016-08-25T08:36:33Z")

</div>

Why not do something like this:

```
Search-ADAccount -LockedOut -SearchBase "OU=xxx,OU=xx,OU=xxx,OU=xxx,DC=xxx,DC=com" | select name | Out-File C:\Test\ADlockout.txt
```

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [August 25, 2016, 8:46am UTC](https://forums.powershell.org/t/writing-properties-of-an-object-to-file/7067/5 "2016-08-25T08:46:54Z")

</div>

You’re going to have a really difficult time building gui’s before learning powershell. Practice getting the results you want in the shell first.

$xyz = Search-ADAccount -LockedOut | select -First 5 | % { $_.name + ’ ----- ’ + $_.samaccountname }  
Load-ListBox $listbox1 $xyz

---

<div class="post-metadata">

**Author:** ![wilfredo-perez](https://avatars.discourse-cdn.com/v4/letter/w/e99b99/32.png) [@wilfredo-perez](https://forums.powershell.org/u/wilfredo-perez)\
**Post date:** [August 25, 2016, 8:53am UTC](https://forums.powershell.org/t/writing-properties-of-an-object-to-file/7067/6 "2016-08-25T08:53:15Z")

</div>

I agree with Dan. Learn and understand PowerShell first before moving on.

---

<div class="post-metadata">

**Author:** ![aaron-miller](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@aaron-miller](https://forums.powershell.org/u/aaron-miller)\
**Post date:** [August 25, 2016, 9:39am UTC](https://forums.powershell.org/t/writing-properties-of-an-object-to-file/7067/7 "2016-08-25T09:39:41Z")

</div>

Well looks like I did have it all along…

```
$xyz = (Search-ADAccount -LockedOut) | % {"$($_.name), $($_.samaccountname)"}
		Load-ListBox $LockedList $xyz
```

This worked, but only after closing/reopening my PS window

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [August 25, 2016, 9:39am UTC](https://forums.powershell.org/t/writing-properties-of-an-object-to-file/7067/8 "2016-08-25T09:39:57Z")

</div>

Note: there are some people that will put you on their kill list for using % in script:D

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [August 25, 2016, 3:33pm UTC](https://forums.powershell.org/t/writing-properties-of-an-object-to-file/7067/9 "2016-08-25T15:33:04Z")

</div>

Add-KillList -Name ertuu85

🙂

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [August 25, 2016, 5:14pm UTC](https://forums.powershell.org/t/writing-properties-of-an-object-to-file/7067/10 "2016-08-25T17:14:36Z")

</div>

😃 using it twice moves you to the top of the list.

---

<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:40pm UTC](https://forums.powershell.org/t/writing-properties-of-an-object-to-file/7067/11 "2024-05-16T20:40:39Z")

</div>


