# Output filename using Returned value

**URL:** https://forums.powershell.org/t/output-filename-using-returned-value/11914
**Category:** PowerShell Help
**Created:** [January 16, 2019, 4:53pm UTC](https://forums.powershell.org/t/output-filename-using-returned-value/11914 "2019-01-16T16:53:42Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![joeo](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@joeo](https://forums.powershell.org/u/joeo)
#### Post date: [January 16, 2019, 4:53pm UTC](https://forums.powershell.org/t/output-filename-using-returned-value/11914/1 "2019-01-16T16:53:42Z")

</div>

Hi,

Its probably a simple question. I would like to retrieve a specific AD value for All AD users, and output the results to a \<SamAccountName\>.txt file. I also want to make sure the output is trimmed. Its probably a For-Each to cycle through all users. So for example, say i wanted to retrieve the “logonCount” for user JSmith. The manual command would be:

(Get-ADUser JSmith -Properties \* | select logonCount | ft -HideTableHeaders | Out-String).trim() | Out-File Jsmith.txt

---

<div class="post-metadata">

### Author: ![conor](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@conor](https://forums.powershell.org/u/conor)
#### Post date: [January 16, 2019, 5:11pm UTC](https://forums.powershell.org/t/output-filename-using-returned-value/11914/2 "2019-01-16T17:11:18Z")

</div>

HI Joe,

&nbsp;

You can acheive this by piping the output of the Get-ADUser cmdlt into a Select-Object command with the -Property parameter like this

&nbsp;

```
Get-ADuser -filter * -properties logoncount | `
select-object -property Samaccountname,logoncount | `
ConvertTo-Csv -NoTypeInformation | `
Select-Object -Skip 1 | `
Out-File Userlogins.txt
```

This also strips out the header while still keeping the results as an object that gets piped from each command

&nbsp;

---

<div class="post-metadata">

### Author: ![joeo](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@joeo](https://forums.powershell.org/u/joeo)
#### Post date: [January 16, 2019, 5:24pm UTC](https://forums.powershell.org/t/output-filename-using-returned-value/11914/3 "2019-01-16T17:24:29Z")

</div>

Thanks for the response

In my case, i need each users count to be outputted to a separate file, saved as SamAccountName.txt and contain “just” the logon count value (e.g. 100)

&nbsp;

---

<div class="post-metadata">

### Author: ![conor](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@conor](https://forums.powershell.org/u/conor)
#### Post date: [January 16, 2019, 6:00pm UTC](https://forums.powershell.org/t/output-filename-using-returned-value/11914/4 "2019-01-16T18:00:40Z")

</div>

No worries,

That is fairly straightforward to accomplish, just not in a one-liner

```
$users = Get-ADuser -filter * -properties logoncount | select-object -property Samaccountname,logoncount

foreach ($user in $users)
    {
        $user.logoncount | out-file "$($user.Samaccountname).txt"
    }
```

&nbsp;

This time i have saved the results from the Get-ADUser cmdlet and used the Select-object cmdlet to filter the returning object so that it only contains two properties;

Logoncount and SamAccountName

I have then just looped through the object and exported the Logoncount property for each object and used the SamAccountName property to name the file.

&nbsp;

---

<div class="post-metadata">

### Author: ![joeo](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@joeo](https://forums.powershell.org/u/joeo)
#### Post date: [January 17, 2019, 2:37pm UTC](https://forums.powershell.org/t/output-filename-using-returned-value/11914/5 "2019-01-17T14:37:25Z")

</div>

Awesome! This is perfect. Just one last question. How do i get it to trim (slimier to the -skip 1). For example, if use this method to retrieve PasswordLastSet, then the text file has the first line empty. The results is on the second line.

---

<div class="post-metadata">

### Author: ![conor](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@conor](https://forums.powershell.org/u/conor)
#### Post date: [January 17, 2019, 4:29pm UTC](https://forums.powershell.org/t/output-filename-using-returned-value/11914/6 "2019-01-17T16:29:28Z")

</div>

That is odd, i dont know why its putting it on the second line.

It may have something to do with how the PasswordLastSet property is displayed, and because we are just writing the output of that property to a file.

&nbsp;

Lets try doing it properly by using the New-item cmdlet.

&nbsp;

```
$users = Get-ADuser -filter * -properties PasswordLastSet | select-object -property Samaccountname,PasswordLastSet

foreach ($user in $users)
    {
       new-item -path "$($user.Samaccountname).txt" -Value $user.PasswordLastSet
    }
```

&nbsp;

That looks like it is working the way you need it.

May i ask why you need individual files with single properties?

It would be much easier to gather the data you want and output it into a single CSV with the named headers you need.

---

<div class="post-metadata">

### Author: ![joeo](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@joeo](https://forums.powershell.org/u/joeo)
#### Post date: [January 21, 2019, 3:00pm UTC](https://forums.powershell.org/t/output-filename-using-returned-value/11914/7 "2019-01-21T15:00:15Z")

</div>

Thanks Conor! This solution seems to work well! Im testing a few scenarios.

I just needed to add “-Type file”

---

<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:34pm UTC](https://forums.powershell.org/t/output-filename-using-returned-value/11914/8 "2024-05-16T20:34:04Z")

</div>


