first time asking a question here. I am new to Powershell and trying to use it to get the last write time of several files with paths I have saved in a .txt file. I have to do this for ~9000 CAD files and am currently practicing. Below is what I have gotten to work, but when I try and write it to a file it gives me an error and I don't know how to fix it.
This is what I have working:
> foreach($line in get-content
> c:\users\jcuthbertson\desktop\filesforgettingdate.txt) {
> if($line -match $regex){
> -join((get-item $line).lastwritetime,",", (get-item $line).name)}}
6/24/2020 11:38:42 AM,Book1.xlsx
6/30/2020 4:16:47 PM,Book2.xlsx
7/10/2020 7:37:31 AM,dwg_vwx_mcd files.xlsx
7/7/2020 9:43:30 AM,Program cleaning flow sequences.xlsx
7/9/2020 8:49:14 AM,vxw paths commas.xlsx
But when I add the "out-file" command it gives me and error saying there is an empty pipe
> foreach($line in get-content
> c:\users\jcuthbertson\desktop\filesforgettingdate.txt) {
> if($line -match $regex){
> -join((get-item $line).lastwritetime,",", (get-item $line).name)}} | out-file c:\users\jcuthbertson\desktop\testdatawrite.txt
At line:3 char:68
+ ... get-item $line).lastwritetime,",", (get-item $line).name)}} | out-fil ...
+ ~
An empty pipe element is not allowed.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : EmptyPipeElement
Any help is greatly appreciated! Thank you!
foreachscript block is not actually sending anything to the pipeline. It is the code inside that has that capability. You either need to move yourOut-Filecommand with-Appendto inside of yourforeachor capture the output before sending toOut-File. You can use a variable or sub-expression operator$(). Variable ->$var = foreach ($line in ...) {...}; $var | out-file. Sub-expression ->$(foreach ($line in ..) {..}) | Out-File.