Insert timestamp into filename when exporting into .csv - windows

I am trying to insert the timestamp into the filename of an output file generated by PowerShell scrip.
So instead of filelist.csv, it names the output file with the actual timestamp (of the outpufile's modification time), like YYYYMMDD_HHMMSS_filelist.csv.
Get-childitem -recurse -file | select-object #{n="Hash";e={get-filehash -algorithm MD5 -path $_.FullName | Select-object -expandproperty Hash}},lastwritetime,length,fullname | export-csv filelist.csv -notypeinformation
Any suggestions as to what is the missing code from the above line for timestamping the output file?

Change:
export-csv filelist.csv -notypeinformation
To:
Export-Csv "$((Get-Date).ToString("yyyyMMdd_HHmmss"))_filelist.csv" -NoTypeInformation
Edit - .ToString() and -UFormat, generally
When using .ToString() or -Format/-UFormat, you are passing a DateTime object and getting back a formatted string. In both cases you have to specify the format of the string you are after. The ways this format is specified differs.
Check out this list of .NET format strings that can be used with ToString()/-Format. Note how e.g. MM is months 01-12, M is months 1-12 and mm is minutes 00-59.
-UFormat is documented to use UNIX formatting. I'm not familair with this at all, but the Notes section goes into detail. You can see here %m is month while %M

In your file name, do as follows:
Export-Csv -Path "$(Get-Date -UFormat '%Y%m%d_%H%M%S')_filelist.csv" -NoTypeInformation

Related

PowerShell Output Filenames of a Specific Extension to a Text File

Currently, I'm using the following command to output filenames to a text file:
( Get-ChildItem -File ).BaseName | Out-File "Track List.txt" -Encoding utf8
How can I limit the filenames to a specific extension, e.g., .mp3?
The -Filter paramater accepts wildecards - ? for any single character, and * for zero or more matches
( Get-ChildItem -File -Filter *.mp3).BaseName | Out-File "Track List.txt" -Encoding utf8
See the Powershell documentaiton on -Filter and wildcards for more information
[wildcards]:

Renaming with last save date

I have a problem with renaming files. My goal is renaming files with the last saving date. Unfortunately, by a wrong modification, the last modified dates are not the same as the last save date.
I tried to solve with this code in Powershell:
Get-ChildItem | Rename-Item -NewName {$_.basename + " " + $_.LastWriteTime.ToString("yyyy-MM-dd") + $_.Extension}
It would be great if the code put the last save date in the end of the filename. But I cant modify it to be correct.
I can sort the files in the File Explorer by last save date. I need these dates to be put in the of the filename.
Anyone could help me solve this?
Try using Get-Date to convert the file's LastWriteTime to a string.
Get-ChildItem | Rename-Item -NewName {$_.basename + " " + (Get-Date $_.LastWriteTime -Format "yyyy-MM-dd") + $_.Extension}

Bigger txt file with less rows on Windows after Powershell Select-String

I applied several filters to a text files using Powershell Get-Content and the -nomatch operator and then i spooled the result to a file.
gc file.txt | {?_ -notmatch 'excl1|excl2|excl3'} | out-file newfile.txt
What happens is that the output file (newfile.txt) has less lines, but it is reported by windows with a bigger size than file.txt.
Has someone ever encountered this behavior? How can I have the correct size reported by windows? I checked the number of rows, the file with less rows is reported as bigger in size.
I'm certain you have an encoding issue. By default Get-Content uses ascii whereas Out-File uses Unicode.
From TechNet
-Encoding
Specifies the type of character encoding used in the file. Valid values are "Unicode", "UTF7", "UTF8", "UTF32", "ASCII", "BigEndianUnicode", "Default", and "OEM". "Unicode" is the default.
Use -Enconding ascii with Out-File or just use Set-Content as it is the partner of Get-Content.
Get-Content file.txt | {?_ -notmatch 'excl1|excl2|excl3'} |
out-file -Encoding ascii newfile.txt
# or
Set-Content newfile.txt
Coming from the other direction if you have issues with your input file Get-Content in PowerShell v3 and above also supports -Enconding

Windows : How to list files recursively with size and last access date?

I need a simple way to create a list of all files in a certain folder. (recursively)
Each file must be in a single line. I also need the file size and the last access date in the same line, separated by a special character.
The output (textfile) should look like this:
c:\folder\file1.txt|400|2012-11-12 15:23:08
c:\folder\file2.txt|200|2012-11-12 15:23:08
c:\folder\file3.txt|100|2012-11-12 15:23:08
c:\folder\sub folder\file4.txt|500|2012-11-12 15:23:08
'Dir' seems not to be an option, because the German Special characters get messed up that way. (öäüß)
Powershell handles the special characters well, but I couldn't make it so that the information for one file ends up in a single line:
get-childitem D:\temp -rec | where {!$_.PSIsContainer} | foreach-object -process {$_.FullName, $_.LastWriteTime, $_.Length}
try this:
get-childitem D:\temp -rec | where {!$_.PSIsContainer} |
select-object FullName, LastWriteTime, Length | export-csv -notypeinformation -delimiter '|' -path file.csv

replacing text in app.config using powershell script

I am trying a simple replace script to replace text in a app.cofig file. But it just processed and do nothing:
$old = 'Z:\gene'
$new = 'z:\gene\scripts'
Get-ChildItem z:\gene\scripts\Test\App.config -Recurse | Where {$_ -IS [IO.FileInfo]} |
% {
(Get-Content $_.FullName) -replace $old,$new | Set-Content $_.FullName
Write-Host "Processed: " + $_.FullName
}
Any Idea what I am doing wrong. As same script works fine for .txt file
Thanks
App.config is xml formatted but it's a text file as well, it should work the same. My guess is that you have a different values that your working on and they are not hitting. If you rename the file to app.txt does it work ? You might also consider using nant xmlpoke if you are running from nant script.

Resources