Docfxit
Docfxit

Reputation: 165

How to write the console output from powershell to a text file

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

Upvotes: 1

Views: 6942

Answers (2)

Scepticalist
Scepticalist

Reputation: 3923

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

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

Should read

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

Upvotes: 1

CaptainBraddles
CaptainBraddles

Reputation: 11

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.

Upvotes: 1

Related Questions