# Foreach output-file

**URL:** <https://forums.powershell.org/t/foreach-output-file/768>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/foreach-output-file/768 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/foreach-output-file/768/1 "2011-12-31T18:30:00Z")

</div>

by xtree79 at 2013-03-22 04:11:08

> Hi everyone,  
>   
> Here is my problem, when i’m try to use out-file cmd for $destFilename in my loop i m not getting the results pushed to the text file.  
> Now if replace out-put with write-host cmd, I get all my results inside the console window.  
> Thing i have tried are :  
> input-object = this only will give me 1 sting line result   
>   
> thanks in advance  
>   
>   
>   
> `# PowerShell Foreach-Object$findDir =‘C:\Users\folder\Desktop\Clients\folder’$LogPath = "C:\Users\folder\Desktop\Clients\powershell1&quot;$FilesExe = gci $findDir -recurse$LogPath = "C:\Users\folder\Desktop\Clients\powershell1&quot;$filetype = "test.txt"$outFile = $LogPath + $filetype$insert3 = $FilesExe | ? {$.extension -eq ".zpl"} $insert4 = "base64 -d"$insert5 = "\output"$insert6 = "&quot;foreach ($file in $insert3){$ParentPath = Split-Path $file.fullname -parent$folderspace = ’ '$destFilename = $insert4 + $folderspace + $ParentPath + $insert6+ $file.name + $folderspace + $ParentPath + $insert5 + $insert6 + $file.nameout-file -filepath $outFile -enc Default -append }`

_by poshoholic at 2013-03-22 05:48:53_

_> In your current script, Out-File does not have any input. You need to use -InputObject or pipe input into Out-File in order to write data to a file.  
>   
> Also, your script can be simplified quite a bit. Take a look at this version as an example (note that it requires PowerShell 3, but with one modification it could be made to work with PowerShell 2):  
> `# Identify the directory to search$findDir = 'C:\Users\folder\Desktop\Clients\folder'# Identify the path to output to$logFile = 'C:\Users\folder\Desktop\Clients\powershell1\test.txt'# Get all zpl files inside $findDir, even if they are hidden (-Force finds hidden files)foreach ($zplfile in Get-ChildItem -LiteralPath $findDir -Recurse -Force -File -Filter '*.zpl') { # Identify the location where you want the decoded file to go $decodedFileFolder = Join-Path -Path $zplfile.DirectoryName -ChildPath output $decodedFilePath = Join-Path -Path $decodedFileFolder -ChildPath $zplfile.Name # Identify the base64 command to decode the file $base64Cmd = "base64 -d $($zplFile.FullName) $decodedFilePath" # Now output the base64 decode command to a file $base64Cmd | Out-File -FilePath $logFile -Append}`_

_by xtree79 at 2013-03-24 08:42:19_

> _Kirk,  
> thanks for your input on how to make my script more simplified , but after doing some more research i figure out how to get the results that i m looking for._  
>   
>   
>   
> _`# PowerShell Foreach-Object$findDir =‘C:\Users\folder\Desktop\Clients\heartland’$LogPath = "C:\Users\folder\Desktop\Clients\powershell1&quot;$FilesExe = gci $findDir -recurse$LogPath = "C:\Users\folder\Desktop\Clients\powershell1&quot;$filetype = "test.txt"$outFile = $LogPath + $filetype$insert3 = $FilesExe | ? {$`_`.extension -eq ".zpl"} $insert4 = "base64 -d"$insert5 = "\output"$insert6 = "&quot;$Results = @()foreach ($file in $insert3){$ParentPath = Split-Path $file.fullname -parent$folderspace = ’ '$Results += $insert4 + $folderspace + $ParentPath + $insert6+ $file.name + $folderspace + $ParentPath + $insert5 + $insert6 + $file.name } $Results | out-file -filepath $outFile -append`

---

<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:57pm UTC](https://forums.powershell.org/t/foreach-output-file/768/2 "2024-05-16T20:57:51Z")

</div>


