none
How to read in text file and output modified data in a new text file RRS feed

  • Question

  • I have the following code which will output the unique lines to terminal but I want them stored in a new text file. How can I accomplish this?

    #=========================================================

    $InputFile = Get-Content Filenet_Cost_Report.txt | Sort-Object | Get-Unique

    $x=1

    foreach ($Line in $InputFile)
     {
            Write-Host $x " " $Line        
            $x++
      }

    #=========================================================

    Thursday, June 28, 2012 8:58 PM

Answers

All replies

  • Add-Content -path "c:\myfile.txt" -value "$x $Line"

    • Proposed as answer by Bigteddy Friday, June 29, 2012 5:52 AM
    • Marked as answer by Yan Li_Moderator Friday, June 29, 2012 9:52 AM
    Thursday, June 28, 2012 9:06 PM
  • Thanks

    Thursday, June 28, 2012 9:43 PM
  • I have another question when I run the following command I get the error that it is not a recogniced cmdlet.

    $Members = Get-ADUser -Filter * -SearchBase "OU=JSN-Filenet-UI-Tax,OU=GROUPS,OU=JSN,DC=MN,DC=EDU"

    In the previous example the following line worked.

    $InputFile = Get-Content Filenet_Cost_Report.txt | Sort-Object | Get-Unique

    What is the difference?

    Friday, June 29, 2012 1:01 PM
  • Do you have the ActiveDirectory module loaded, you need it if you're to run the cmdlet 'Get-ADUser' ? You can run 'Get-Module -ListAvailable' to verify.

    PS O:\joey\scripts> Get-Module -ListAvailable
    
    ModuleType Name                      ExportedCommands
    ---------- ----                      ----------------
    .
    ..              
    Manifest   ActiveDirectory           {}
    ....
    .....

    Friday, June 29, 2012 4:04 PM
  • ModuleType Name                      ExportedCommands
    ---------- ----                      ----------------
    Manifest   ActiveDirectory           {}
    Manifest   BitsTransfer              {}
    Manifest   GroupPolicy               {}
    Manifest   PSDiagnostics             {}
    Manifest   TroubleshootingPack       {}

    This is my output when I run the Get-Module -ListAvailable command.

    Friday, June 29, 2012 8:54 PM