Powershell: Copy files and additional files with different name

Powershell Header

This maybe helps some people which need to copy mutiple files. In my example I search for Contoso in files called info_*****.txt I need to copy them and also copy the file data_*****.txt.

$sourceFolder = "E:\temp\source"
$destinationFolder = "E:\temp\folder1"

$files = Get-ChildItem $sourceFolder -Filter *.txt -Recurse | Select-String "Contoso" # Get all Files with Contoso
Write-Host "Files found: " $files.count # Number of files found
foreach ($file in $files){
Get-Childitem $sourceFolder | Where-Object { $_.name -eq $file.filename } | Copy-Item -Destination $destinationFolder # copy all info_*****.txt files
$name = $file.filename -replace "info_", "data_"
Get-Childitem $sourceFolder | Where-Object { $_.name -eq $name } | Copy-Item -Destination $destinationFolder # copy all data_*****.txt files
}

More Infos about Select-String and file copy

Powershell: Copy Logfiles with Date and Content

Powershell Header

Some days ago I made a blog post about Select-String in Powershell. I created a “script” or a better a command to check logfiles for a specitifc text and copy the logfile to another place.

Get-ChildItem C:\temp -Filter *.log -Recurse | Select-String "Contoso" |  Copy-Item -Destination C:\temp2

Now I added also a Startdate and a Enddate for the logfiles you wanna search in.

$Startdate = (get-date -year 2011 -month 3 -day 25)
$Enddate = (get-date -year 2011 -month 3 -day 30)

Get-ChildItem C:\temp -Filter *.log -Recurse | Where-Object {($_.LastWriteTime.Date -ge $Startdate.Date) -and ($_.LastWriteTime.Date -le $Enddate.Date)} | Select-String "Contoso" |  Copy-Item -Destination C:\temp2

Powershell: Search for String or grep for Powershell

Powershell Header

This shows you how you can search in files for a specific content with Windows Powershell. This “replaces” the Windows command-line utility “findstr”. In the Unix/Linux world you mostly use the command grep for doing the same.

grep syntax

grep (options) files.txt

grep example

grep "text I search" *.log

In Windows Powershell we can use the Select-String to search strings in files

Select-String -Path C:\temp\*.log -Pattern "Contoso"

If you need some more options, for example you need also check subfolders (-Recurse) or you need additional filter for files you wanna check, you can use the Get-Childitem first.

Get-ChildItem C:\temp -Filter *.log -Recurse | Select-String "Contoso" 

If you have to copy all the files with a specific content, you can simply add a Copy-Item cmdlet.

Get-ChildItem C:\temp -Filter *.log -Recurse | Select-String "Contoso" | Copy-Item -Destination C:\temp2

More Information about Select-String:

Continue reading

Powershell: Count your Code lines

Powershell Header

After Coding some lines in a lot of different files you wanna know how much lines you have coded. There are two (I am sure there are even more) ways to do that. The first one is to get the content of the files (Get-Content) and count the lines in there.

The other way and the fasterway is with Select-String:

(Get-ChildItem -Include *.ps1 -Recurse | Select-String -pattern .).Count