# List files in a folder
Get-ChildItem "C:\temp"
# List all text files in a folder recursively (include subfolders)
Get-ChildItem "C:\temp\*.txt" -recurse
# What does $_ mean in PowerShell?
This is the variable for the current value in the pipe line, which is called $PSItem in Powershell 3 and newer. For example in the below code the %{} block is called for every value in the array. The $_ or $PSItem variable will contain the current value.
1,2,3 | ForEach-Object {Write-Host $_}
% is alias for ForEach-Object so above code can be written as below
1,2,3 | % {Write-Host $_}
# Remove lines starting with "--" from multiple files
Get-ChildItem "C:\Program Files\xMS\LogFiles\DailyLogs\*.xme" -recurse | ForEach{(Get-Content $_ | Where { $_ -notmatch "^--" }) | Set-Content $_}
# Remove lines starting with "Rec" from multiple files
Get-ChildItem "C:\Program Files\xMS\LogFiles\DailyLogs\*.xme" -recurse | ForEach{(Get-Content $_ | Where { $_ -notmatch "^Rec" }) | Set-Content $_}
# Insert a line at top of multiple files
$header = "Received,Occurred,Closed,Location,Element,Severity,Source,Type,Status,Ack,Seq. #,User,Equipment Type" Get-ChildItem "C:\Program Files\xMS\LogFiles\DailyLogs\" -Recurse -Filter *.xme| Foreach-Object { @($header;Get-Content $_.FullName) | Set-Content -Path $_.FullName}
For big files, following is a faster way to insert header at top of multiple files
$header = "Received,Occurred,Closed,Location,Element,Severity,Source,Type,Status,Ack,Seq. #,User,Equipment Type" $files = Get-Childitem "C:\Users\shariramani\Desktop\delete\xme\*.csv" foreach ($file in $files) { $header | Set-Content tempfile.txt Get-Content $file -ReadCount 5000 | Add-Content tempfile.txt Remove-item $file Rename-Item tempfile.txt -NewName $file }
# Apend a string or line to all text files with an exception
This code appends a line to all text files in the specified directory but excludes files based on their file name.
Add-Content -Path "C:\temp\*.txt" -Exclude help* -Value 'End of file'
Append a line using variable
$myline="End of File" Add-Content -Path "C:\temp\*.txt" -Exclude help* -Value $myline
# Remove empty lines
Get-ChildItem "C:\Program Files\xMS\LogFiles\DailyLogs\*.xme" -recurse | ForEach{(Get-Content $_ | Where {$_.trim() -ne "" }) | Set-Content $_}
# Replace TAB with comma
Get-ChildItem "C:\Program Files\xMS\LogFiles\DailyLogs\*.xme" -recurse | ForEach{(Get-Content $_ | ForEach {$_ -replace "`t", “,”}) | Set-Content $_}
# Rename multiple files
This code renames file extension from filename.xme to filename.csv
Get-ChildItem "C:\Program Files\xMS\LogFiles\DailyLogs\*.xme" | Rename-Item -NewName { $_.name -Replace '\.xme$','.csv' }
# Windows Powershell Script to Delete Files Created Before X days
I am an IOT enthusiast with more than 20 years of experience in the IT sector. Specializing in telecom service's; follow me for some very innovative and best in class IOT products as I unfold my knowledge and passion for the subject.
Just me, myself and I, exploring the universe of uknownment. I have a heart of love and interests in technology, IOT and travel . And I want to share my world with you .