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

Welcome Forums General PowerShell Q&A Why doesn't out-File write to a file?

Viewing 5 reply threads
  • Author
    Posts
    • #183375
      Participant
      Topics: 2
      Replies: 4
      Points: 1
      Rank: Member

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

      $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
    • #183393
      Participant
      Topics: 10
      Replies: 117
      Points: 456
      Helping Hand
      Rank: Contributor

      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.

    • #183429
      Participant
      Topics: 1
      Replies: 1632
      Points: 3,074
      Helping Hand
      Rank: Community Hero

      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?

    • #183663
      Participant
      Topics: 2
      Replies: 4
      Points: 1
      Rank: Member

      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,

    • #183675
      Participant
      Topics: 10
      Replies: 117
      Points: 456
      Helping Hand
      Rank: Contributor

      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 ... | ...

    • #183678
      Participant
      Topics: 3
      Replies: 68
      Points: 367
      Helping Hand
      Rank: Contributor

      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.

Viewing 5 reply threads
  • You must be logged in to reply to this topic.