Disclaimer
The PowerShell code in this educational post is a framework intended only for the demonstration of PowerShell functionalities. The creator does not consider this work to be of production quality.
@andyh - A function’s metadata and parameter attributes can provide much of the feedback you indicated. The following example is only a starting point for your exploration into PowerShell’s built-in capabilities. As @Olaf states, building understanding and skills in PowerShell is an iterative process --and, there should be much reading. However, long ago, someone gave me a programmatic scaffold on which I could develop.
Example
search files in a folder on multiple strings… (OP)
matches on any one of the 3 lookups copy the file found to a archive folder… (OP)
Remarks
- Uses
Move-Item
due to the archival nature in the process (replaces Copy-Item). -
return
safely exits the function, but does not close the PowerShell session (replaces Exit).- The function completes as soon as a match is made.
- Parameter validation provides detailed information to the user (replaces Write-Host or Write-Warning).
- Optional parameters are provided for source and target directories. Their pattern validation allows for child directories, but not files (-ish. I’m pretty sure this regex could be cheated).
- The keyword search is not finely tuned. The match is more literal, but unexpected results will occur.
- Follow @Olaf’s suggestions. The use of the
Keywords
array can be expanded on prior to the search.
- Follow @Olaf’s suggestions. The use of the
<#
.Synopsis
Move a file when a keyword is found in the contents.
.DESCRIPTION
This function moves a file to a new location when at least one of three keywords is found within the file contents.
.EXAMPLE
Move-FileToArchive -Keywords "Alice","210328" -SourcePath "A:\SourceDirectory" -TargetPath "A:\TargetDirectory";
.EXAMPLE
Move-FileToArchive -Keywords "Alice";
.EXAMPLE
Move-FileToArchive -Keywords "Alice","210328","Bob";
.INPUTS
Keywords (an array of 1 to 3 members)
Searched file directory (SourcePath)
Destination file directory (TargetPath)
.NOTES
File is moved is any one keyword is found in the content.
Use Filedate format yymmdd in datetime searches.
#>
function Move-FileToArchive
{
[CmdletBinding(PositionalBinding=$true,
HelpUri = "Keywords should include: Sender, FileDate, or RecordType",
ConfirmImpact='low')]
Param
(
[Parameter(Mandatory=$true,Position=0)]
[ValidateCount(1,3)]
[array]$Keywords,
[Parameter(Mandatory=$false,Position=1)]
[ValidatePattern("^(?:[\w]\:|\\)(\\[a-z_\-\s0-9\.]+)+$")]
[string]$SourcePath,
[Parameter(Mandatory=$false,Position=2)]
[ValidatePattern("^(?:[\w]\:|\\)(\\[a-z_\-\s0-9\.]+)+$")]
[string]$TargetPath
)
Begin
{
if([string]::IsNullOrWhiteSpace($SourcePath)){$SourcePath = "A:\Backups"}
if([string]::IsNullOrWhiteSpace($TargetPath)){$TargetPath = "A:\Backups\TargetArchive"}
}
Process
{
foreach($file in Get-ChildItem -Path $SourcePath -Filter "*.bak")
{
[string]$searchFile = "{0}\{1}"-f $SourcePath,$file.Name;
[string]$content = Get-Content -Path $searchFile;
foreach($term in $Keywords)
{
if($content -match $term)
{
return Move-Item -Path $searchFile -Destination $TargetPath;
}
}
}
}
}