1

I have tried many solutions to get the output of powershell to a text file so I can read it. I can't get the console output to stop at the end so I can read it and I can't get it to write out a file. With this code a text file isn't written. Windows 10 1903

:: This will Remove all Appxpackages
::
$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'

C:\Batch\PSEXEC.EXE  -s powershell -c 
Start-Transscript -Path 'C:\RemoveAllAppxPackages.txt'
ForEach ($App in $AppsList){
    $PackageFullName = (Get-AppxPackage -AllUsers $App).PackageFullName
    $ProPackageFullName = (Get-AppxProvisionedPackage -AllUsers | where {$_.Displayname -eq                         $App}).PackageName
write-host $PackageFullName
Write-Host $ProPackageFullName
if ($PackageFullName){
    Write-Host "Removing Package: $App"
    remove-AppxPackage -package $PackageFullName -AllUsers  > C:\RemoveAllAppxPackages.txt
)   pause
   }

    else{
        Write-Host "Unable to find package: $App"  > C:\RemoveAllAppxPackages.txt
   pause
    }

    if ($ProPackageFullName){
        Write-Host "Removing Provisioned Package: $ProPackageFullName"
        Remove-AppxProvisionedPackage -online -packagename $ProPackageFullName  >             C:\RemoveAllAppxPackages.txt
   pause
    }

    else{
        Write-Host "Unable to find provisioned package: $App"  > C:\RemoveAllAppxPackages.txt
   pause
    }
}
Pause
Stop-Transcript

2 Answers 2

1

Instead of your write-host line, you could just write the bits you need in the file (wrap in quotes) and then pipe to out-file to a txt file. eg.

"Unable to find package: $App" | Out-File -FilePath C:\path\to\file.txt -Append

Note, make sure you add -append otherwise the file will be overwritten each time.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the suggestion. I have made the changes you suggested. I uploaded it to this link: pastebin.com/CjsGJNCL It isn't writing out the file and I'm still getting something written to the console. Do you have any idea what is wrong? I am running this as Administrator.
The errors were from the operating system. I didn't put the errors on pastebin. There were many changes to the script that I shared on pastebin. Is there a different way for me to share a large change in the code? When I find the correct answer I am happy to replace the code in my first post or post it in the answer.
Even though I followed someone elses example tried separating the bat file from the script. This is what I now have in the bat file: "C:\Batch\PSEXEC.EXE -s PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}" > E:\Dnload\Sysprep\RemoveAllAppxPackages.txt Pause" I changed the script to this: pastebin.com/cLsjMxxq
1

You have a typo in the Start-Transcript command.....

Start-Transscript -Path 'C:\RemoveAllAppxPackages.txt'

Should read

Start-Transcript -Path 'C:\RemoveAllAppxPackages.txt'

6 Comments

Good catch. Thanks. I tried writing an empty file to C:\RemoveAllAppxPackages.txt from a text editor and I received an error saying "you don't have permission to save in this location", I gave the root permissions. Then I tried again and I received another error saying "a required privilege is not held by the client" so I Disable The Admin Approval Mode in the group policy. I can now write to the root of C:\ without running as Admin. Powershell still won't write the output to the txt file.
Don't write to root in scripts, it's a needless complication. Use/create a folder for your files.
I also tried writing to the same folder the script is run from: "E:\Dnload\Sysprep\RemoveAllAppxPackages.txt" and it won't write to that file. It doesn't make any difference if the file is not there before the script is run or if there is a blank file.
There must be a way to see what powershell writes out. It disappears off the screen in a split second.
I found a way to run the script in a powershell window. I fixed a few errors. I can't figure out this error: Get-AppxProvisionedPackage : Parameter set cannot be resolved using the specified named parameters. At E:\Dnload\Sysprep\RemoveAllAppxPackages3.ps1:28 char:28 + $ProPackageFullName = (Get-AppxProvisionedPackage | where {$_.Dis ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-AppxProvisionedPackage], ParameterBindingException
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.