Why doesn't out-File write to a file?

I have a script that I can’t get the output to write to a file. This is the script:

[pre]$AppsList = 'Microsoft.3DBuilder', 'Microsoft.BingFinance', 'Microsoft.BingNews', 'Microsoft.BingSports', 'Microsoft.MicrosoftSolitaireCollection', 'Microsoft.People', 'Microsoft.Windows.Photos', 'Microsoft.WindowsCamera', 'microsoft.windowscommunicationsapps', 'Microsoft.WindowsPhone', 'Microsoft.WindowsSoundRecorder', 'Microsoft.XboxApp', 'Microsoft.ZuneMusic', 'Microsoft.ZuneVideo', 'Microsoft.Getstarted', 'Microsoft.WindowsFeedbackHub', 'Microsoft.XboxIdentityProvider', 'Microsoft.MicrosoftOfficeHub', 'Fitbit.FitbitCoach', 'ThumbmunkeysLtd.PhototasticCollage', 'Microsoft.People', 'king.com.CandyCrushFriends'

#Start-Transcript -Path ‘E:\Dnload\Sysprep\RemoveAllAppxPackages.txt’ -append
ForEach ($App in $AppsList){
$PackageFullName = (Get-AppxPackage -AllUsers “$App”).PackageFullName
$ProPackageFullName = (Get-AppxProvisionedPackage -online | where {$_.Displayname -eq $App}).PackageName
$PackageFullName | out-File -FilePath E:\Dnload\Sysprep\RemoveAllAppxPackages.txt -Append
$ProPackageFullName | out-File -FilePath E:\Dnload\Sysprep\RemoveAllAppxPackages.txt -Append
if ($PackageFullName){
“Removing Package: $App” | out-File -FilePath E:\Dnload\Sysprep\RemoveAllAppxPackages.txt -Append
remove-AppxPackage -package $PackageFullName -AllUsers | out-File -FilePath E:\Dnload\Sysprep\RemoveAllAppxPackages.txt -Append
}

else{
“Unable to find package: $App” | out-File -FilePath E:\Dnload\Sysprep\RemoveAllAppxPackages.txt -Append
}

if ($ProPackageFullName){
“Removing Provisioned Package: $ProPackageFullName” | out-File -FilePath E:\Dnload\Sysprep\RemoveAllAppxPackages.txt -Append
Remove-AppxProvisionedPackage -online -packagename $ProPackageFullName | out-File -FilePath E:\Dnload\Sysprep\RemoveAllAppxPackages.txt -Append
pause
}

else{
“Unable to find provisioned package: $App” | out-File -FilePath E:\Dnload\Sysprep\RemoveAllAppxPackages.txt -Append
pause
}
}
Pause
#Stop-Transcript[/pre]

It would be beneficial if you can repost your code as per https://powershell.org/forums/topic/read-me-before-posting-youll-be-glad-you-did/ so it’s more readable and you can get a quicker answer.

I ran your script - of course with another output path and of course without the remove cmdlets but I’ve just got the expected output written to the output file I specified.
Do you get errors?

Thank you for running the script.

I don’t get any errors. It doesn’t create the output file. And If I create a blank file it doesn’t write to it.

Do you have any idea how I could figure out what the problem is?

Thanks,

What if you try to manually create the file in the same path using New-Item and pipe a simple string to it?

Try using some try/catch around Remove-AppxProvisionedPackage … | …

Potential problems:
Location - Is E:\ a local drive, or a network drive? If it is a network drive, you may have to tell PowerShell where it is, like this: https://stackoverflow.com/questions/4742992/cannot-access-network-drive-in-powershell-running-as-administrator Also, consider testing your script with a different output location, such as ~\test.txt (local user directory).
Permissions - Have you verified that the user account running the script definitely has permission to write files at the target location? Permissions cause headaches all the time.

If those things seem fine, try setting this:

Set-PSDebug -Trace 2

and then re-run your script. Reading through the output should tell you what is and isn’t happening while your script executes.