First post here, so if I am violating any rules, please forgive the transgression.
I am learning PS from a book that was given to me. I am at the end of the first chapter and have a question about one of the labs (sounds ominous that I am only at Ch 1 and already asking for help).
The task: Create a .txt file named mydir.txt of the C:Windows directory. I will place this in F:Test
So I know I can create the desired result via the following method:
PS C:Windows> dir > F:Testmydir.txt
I am trying to play around with different methods using the newer cmdlets. Here is what I am trying:
PS C:Windows> get-childitem | new-item -path F:Test -name mydir.txt -type "file"
I know get-childitem by itself will display the entire directory on the screen. I also know that if I use the new-item portion (obtained from the get-help new-item -examples) by itself, I can create a blank text file in the appropriate location with the specified name.
The problem I am running into is when I put the two together. The result is that the file is successfully created in the desired directory and with the desired name, however, within the .txt file is only the very first item of the directory listing for C:Windows. For each subsequent item in the directory, I get the error "The File 'F:Testmydir.txt' already exists."
How do I get all of the get-childitem results to output into the one file? Get-childitem seems to try and take each directory item individually and output them to it's own new-item file. Is there a way to have it treat the directory items collectively as a "single item" for lack of a better term and put them into the single new-item file?
I hope this makes sense. Thank you for your time,
P.S. So I had a moment of clarity right at the end of writing the big explanation above…so I just discovered that:
PS C:Windows> get-childitem > F:Testmydir.txt
will also give me the desired result. Didn't know that you could mix the old CMD commands with the new cmdlets (but now that I think about it it makes sense, since they are aliased.) My fault in understanding seems to lie with the piping | function? I think.