Show output on screen and in file in PowerShell










17















How can I get all this to not only output on the screen, but to save to a text file in CSV format?



$OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" `
-SearchBase "OU=GA,OU=EAST,DC=corp,DC=chm,DC=com" | Select distinguishedName
ForEach ($OU In $OUs)
Select Name



I have tried



$OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" `
-SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" | Select distinguishedName
ForEach ($OU In $OUs)
Select Name
| | export-CSV c:tempoutfile.csv –noType


And many other formats, but I always get the error:




An empty pipe element is not allowed.











share|improve this question




























    17















    How can I get all this to not only output on the screen, but to save to a text file in CSV format?



    $OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" `
    -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chm,DC=com" | Select distinguishedName
    ForEach ($OU In $OUs)
    Select Name



    I have tried



    $OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" `
    -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" | Select distinguishedName
    ForEach ($OU In $OUs)
    Select Name
    | | export-CSV c:tempoutfile.csv –noType


    And many other formats, but I always get the error:




    An empty pipe element is not allowed.











    share|improve this question


























      17












      17








      17


      3






      How can I get all this to not only output on the screen, but to save to a text file in CSV format?



      $OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" `
      -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chm,DC=com" | Select distinguishedName
      ForEach ($OU In $OUs)
      Select Name



      I have tried



      $OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" `
      -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" | Select distinguishedName
      ForEach ($OU In $OUs)
      Select Name
      | | export-CSV c:tempoutfile.csv –noType


      And many other formats, but I always get the error:




      An empty pipe element is not allowed.











      share|improve this question
















      How can I get all this to not only output on the screen, but to save to a text file in CSV format?



      $OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" `
      -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chm,DC=com" | Select distinguishedName
      ForEach ($OU In $OUs)
      Select Name



      I have tried



      $OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" `
      -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" | Select distinguishedName
      ForEach ($OU In $OUs)
      Select Name
      | | export-CSV c:tempoutfile.csv –noType


      And many other formats, but I always get the error:




      An empty pipe element is not allowed.








      powershell






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 6 '15 at 9:34









      Peter Mortensen

      13.7k1986113




      13.7k1986113










      asked Jun 22 '12 at 18:16









      RonRon

      100117




      100117






















          2 Answers
          2






          active

          oldest

          votes


















          35














          Use the Tee-Object cmdlet.




          The Tee-Object cmdlet enables you to display data in the Windows PowerShell window and to save that same data to a text file, all with a single command.




          dir | Tee-Object -file dir.txt


          You should use it like,



           ForEach ($OU In $OUs)
          Select Name
          | Tee-Object -file c:tempoutfile.txt


          Note: It has an alias, tee, which is the same as Unix' tee.






          share|improve this answer

























          • that gives me the same error

            – Ron
            Jun 22 '12 at 18:26











          • $OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" ` -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chm,DC=com" | Select distinguishedName ForEach ($OU In $OUs) $OU.distinguishedName Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel ` -Filter * Tee-Object -file c:tempoutfile.txt

            – Ron
            Jun 22 '12 at 18:27











          • Just pipe it to a Tee-Object command. See my update.

            – Shiplu Mokaddim
            Jun 22 '12 at 18:31











          • I am and i am getting the same error }| Tee-Object -file c:tempoutfile.txt An empty pipe element is not allowed. At line:8 char:3

            – Ron
            Jun 22 '12 at 18:47












          • i got it to work with Tee but i had to (and i will try to use the right terms) wrap that in and maike it a function. I was then able to Tee the function. Thanks

            – Ron
            Jun 22 '12 at 18:57


















          0















          An empty pipe element is not allowed.




          You have a duplicate bar in the last line:



          } | | export-CSV c:tempoutfile.csv –noType


          That doesn't fix the error completely, because you can't pipe directly from the foreach loop. You can export it another way to a CSV file instead of a text file like Tee-Object does (you could convert Tee-Object to CSV). This way, it outputs the results to a CSV file and prints the results to the screen:



          $OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" `
          -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" | Select distinguishedName

          $results = @()
          ForEach ($OU In $OUs)

          $OU.distinguishedName
          Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel `
          -Filter * $results | Export-Csv c:tempoutfile.csv -NoTypeInformation
          write-host $results


          This can also be done with a much simpler script:



          Get-ADComputer -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" -SearchScope OneLevel -Filter * | Select Name | Export-Csv c:tempoutfile.csv -NoTypeInformation
          Import-Csv c:tempoutfile.csv





          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11161929%2fshow-output-on-screen-and-in-file-in-powershell%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            35














            Use the Tee-Object cmdlet.




            The Tee-Object cmdlet enables you to display data in the Windows PowerShell window and to save that same data to a text file, all with a single command.




            dir | Tee-Object -file dir.txt


            You should use it like,



             ForEach ($OU In $OUs)
            Select Name
            | Tee-Object -file c:tempoutfile.txt


            Note: It has an alias, tee, which is the same as Unix' tee.






            share|improve this answer

























            • that gives me the same error

              – Ron
              Jun 22 '12 at 18:26











            • $OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" ` -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chm,DC=com" | Select distinguishedName ForEach ($OU In $OUs) $OU.distinguishedName Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel ` -Filter * Tee-Object -file c:tempoutfile.txt

              – Ron
              Jun 22 '12 at 18:27











            • Just pipe it to a Tee-Object command. See my update.

              – Shiplu Mokaddim
              Jun 22 '12 at 18:31











            • I am and i am getting the same error }| Tee-Object -file c:tempoutfile.txt An empty pipe element is not allowed. At line:8 char:3

              – Ron
              Jun 22 '12 at 18:47












            • i got it to work with Tee but i had to (and i will try to use the right terms) wrap that in and maike it a function. I was then able to Tee the function. Thanks

              – Ron
              Jun 22 '12 at 18:57















            35














            Use the Tee-Object cmdlet.




            The Tee-Object cmdlet enables you to display data in the Windows PowerShell window and to save that same data to a text file, all with a single command.




            dir | Tee-Object -file dir.txt


            You should use it like,



             ForEach ($OU In $OUs)
            Select Name
            | Tee-Object -file c:tempoutfile.txt


            Note: It has an alias, tee, which is the same as Unix' tee.






            share|improve this answer

























            • that gives me the same error

              – Ron
              Jun 22 '12 at 18:26











            • $OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" ` -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chm,DC=com" | Select distinguishedName ForEach ($OU In $OUs) $OU.distinguishedName Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel ` -Filter * Tee-Object -file c:tempoutfile.txt

              – Ron
              Jun 22 '12 at 18:27











            • Just pipe it to a Tee-Object command. See my update.

              – Shiplu Mokaddim
              Jun 22 '12 at 18:31











            • I am and i am getting the same error }| Tee-Object -file c:tempoutfile.txt An empty pipe element is not allowed. At line:8 char:3

              – Ron
              Jun 22 '12 at 18:47












            • i got it to work with Tee but i had to (and i will try to use the right terms) wrap that in and maike it a function. I was then able to Tee the function. Thanks

              – Ron
              Jun 22 '12 at 18:57













            35












            35








            35







            Use the Tee-Object cmdlet.




            The Tee-Object cmdlet enables you to display data in the Windows PowerShell window and to save that same data to a text file, all with a single command.




            dir | Tee-Object -file dir.txt


            You should use it like,



             ForEach ($OU In $OUs)
            Select Name
            | Tee-Object -file c:tempoutfile.txt


            Note: It has an alias, tee, which is the same as Unix' tee.






            share|improve this answer















            Use the Tee-Object cmdlet.




            The Tee-Object cmdlet enables you to display data in the Windows PowerShell window and to save that same data to a text file, all with a single command.




            dir | Tee-Object -file dir.txt


            You should use it like,



             ForEach ($OU In $OUs)
            Select Name
            | Tee-Object -file c:tempoutfile.txt


            Note: It has an alias, tee, which is the same as Unix' tee.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 6 '15 at 9:36









            Peter Mortensen

            13.7k1986113




            13.7k1986113










            answered Jun 22 '12 at 18:20









            Shiplu MokaddimShiplu Mokaddim

            44.5k10104167




            44.5k10104167












            • that gives me the same error

              – Ron
              Jun 22 '12 at 18:26











            • $OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" ` -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chm,DC=com" | Select distinguishedName ForEach ($OU In $OUs) $OU.distinguishedName Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel ` -Filter * Tee-Object -file c:tempoutfile.txt

              – Ron
              Jun 22 '12 at 18:27











            • Just pipe it to a Tee-Object command. See my update.

              – Shiplu Mokaddim
              Jun 22 '12 at 18:31











            • I am and i am getting the same error }| Tee-Object -file c:tempoutfile.txt An empty pipe element is not allowed. At line:8 char:3

              – Ron
              Jun 22 '12 at 18:47












            • i got it to work with Tee but i had to (and i will try to use the right terms) wrap that in and maike it a function. I was then able to Tee the function. Thanks

              – Ron
              Jun 22 '12 at 18:57

















            • that gives me the same error

              – Ron
              Jun 22 '12 at 18:26











            • $OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" ` -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chm,DC=com" | Select distinguishedName ForEach ($OU In $OUs) $OU.distinguishedName Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel ` -Filter * Tee-Object -file c:tempoutfile.txt

              – Ron
              Jun 22 '12 at 18:27











            • Just pipe it to a Tee-Object command. See my update.

              – Shiplu Mokaddim
              Jun 22 '12 at 18:31











            • I am and i am getting the same error }| Tee-Object -file c:tempoutfile.txt An empty pipe element is not allowed. At line:8 char:3

              – Ron
              Jun 22 '12 at 18:47












            • i got it to work with Tee but i had to (and i will try to use the right terms) wrap that in and maike it a function. I was then able to Tee the function. Thanks

              – Ron
              Jun 22 '12 at 18:57
















            that gives me the same error

            – Ron
            Jun 22 '12 at 18:26





            that gives me the same error

            – Ron
            Jun 22 '12 at 18:26













            $OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" ` -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chm,DC=com" | Select distinguishedName ForEach ($OU In $OUs) $OU.distinguishedName Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel ` -Filter * Tee-Object -file c:tempoutfile.txt

            – Ron
            Jun 22 '12 at 18:27





            $OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" ` -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chm,DC=com" | Select distinguishedName ForEach ($OU In $OUs) $OU.distinguishedName Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel ` -Filter * Tee-Object -file c:tempoutfile.txt

            – Ron
            Jun 22 '12 at 18:27













            Just pipe it to a Tee-Object command. See my update.

            – Shiplu Mokaddim
            Jun 22 '12 at 18:31





            Just pipe it to a Tee-Object command. See my update.

            – Shiplu Mokaddim
            Jun 22 '12 at 18:31













            I am and i am getting the same error }| Tee-Object -file c:tempoutfile.txt An empty pipe element is not allowed. At line:8 char:3

            – Ron
            Jun 22 '12 at 18:47






            I am and i am getting the same error }| Tee-Object -file c:tempoutfile.txt An empty pipe element is not allowed. At line:8 char:3

            – Ron
            Jun 22 '12 at 18:47














            i got it to work with Tee but i had to (and i will try to use the right terms) wrap that in and maike it a function. I was then able to Tee the function. Thanks

            – Ron
            Jun 22 '12 at 18:57





            i got it to work with Tee but i had to (and i will try to use the right terms) wrap that in and maike it a function. I was then able to Tee the function. Thanks

            – Ron
            Jun 22 '12 at 18:57













            0















            An empty pipe element is not allowed.




            You have a duplicate bar in the last line:



            } | | export-CSV c:tempoutfile.csv –noType


            That doesn't fix the error completely, because you can't pipe directly from the foreach loop. You can export it another way to a CSV file instead of a text file like Tee-Object does (you could convert Tee-Object to CSV). This way, it outputs the results to a CSV file and prints the results to the screen:



            $OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" `
            -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" | Select distinguishedName

            $results = @()
            ForEach ($OU In $OUs)

            $OU.distinguishedName
            Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel `
            -Filter * $results | Export-Csv c:tempoutfile.csv -NoTypeInformation
            write-host $results


            This can also be done with a much simpler script:



            Get-ADComputer -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" -SearchScope OneLevel -Filter * | Select Name | Export-Csv c:tempoutfile.csv -NoTypeInformation
            Import-Csv c:tempoutfile.csv





            share|improve this answer





























              0















              An empty pipe element is not allowed.




              You have a duplicate bar in the last line:



              } | | export-CSV c:tempoutfile.csv –noType


              That doesn't fix the error completely, because you can't pipe directly from the foreach loop. You can export it another way to a CSV file instead of a text file like Tee-Object does (you could convert Tee-Object to CSV). This way, it outputs the results to a CSV file and prints the results to the screen:



              $OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" `
              -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" | Select distinguishedName

              $results = @()
              ForEach ($OU In $OUs)

              $OU.distinguishedName
              Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel `
              -Filter * $results | Export-Csv c:tempoutfile.csv -NoTypeInformation
              write-host $results


              This can also be done with a much simpler script:



              Get-ADComputer -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" -SearchScope OneLevel -Filter * | Select Name | Export-Csv c:tempoutfile.csv -NoTypeInformation
              Import-Csv c:tempoutfile.csv





              share|improve this answer



























                0












                0








                0








                An empty pipe element is not allowed.




                You have a duplicate bar in the last line:



                } | | export-CSV c:tempoutfile.csv –noType


                That doesn't fix the error completely, because you can't pipe directly from the foreach loop. You can export it another way to a CSV file instead of a text file like Tee-Object does (you could convert Tee-Object to CSV). This way, it outputs the results to a CSV file and prints the results to the screen:



                $OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" `
                -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" | Select distinguishedName

                $results = @()
                ForEach ($OU In $OUs)

                $OU.distinguishedName
                Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel `
                -Filter * $results | Export-Csv c:tempoutfile.csv -NoTypeInformation
                write-host $results


                This can also be done with a much simpler script:



                Get-ADComputer -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" -SearchScope OneLevel -Filter * | Select Name | Export-Csv c:tempoutfile.csv -NoTypeInformation
                Import-Csv c:tempoutfile.csv





                share|improve this answer
















                An empty pipe element is not allowed.




                You have a duplicate bar in the last line:



                } | | export-CSV c:tempoutfile.csv –noType


                That doesn't fix the error completely, because you can't pipe directly from the foreach loop. You can export it another way to a CSV file instead of a text file like Tee-Object does (you could convert Tee-Object to CSV). This way, it outputs the results to a CSV file and prints the results to the screen:



                $OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" `
                -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" | Select distinguishedName

                $results = @()
                ForEach ($OU In $OUs)

                $OU.distinguishedName
                Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel `
                -Filter * $results | Export-Csv c:tempoutfile.csv -NoTypeInformation
                write-host $results


                This can also be done with a much simpler script:



                Get-ADComputer -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" -SearchScope OneLevel -Filter * | Select Name | Export-Csv c:tempoutfile.csv -NoTypeInformation
                Import-Csv c:tempoutfile.csv






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 '18 at 4:10









                Peter Mortensen

                13.7k1986113




                13.7k1986113










                answered Oct 25 '16 at 22:41









                InthedriversseatInthedriversseat

                32




                32



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11161929%2fshow-output-on-screen-and-in-file-in-powershell%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                    Museum of Modern and Contemporary Art of Trento and Rovereto