Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Wednesday, January 18, 2012

Search a hard drive for image files and copy them to another client with PowerShell

Here is another one of the student projects from Decembers PowerShell class.  In this case, the student needed to examine the hard drive of a computer and discover certain image files.  He then needed to copy those files to another computer and hold them in a folder who’s name contained the source clients name and a date stamp.  This code is still rough as we identified a lot of conflict scenarios that could pop up, but it still works.

 

Function Copy-ImageFile
{
param
(
[
switch]$ListFiles,
$Destination = "C:\Users\Jason\Documents\Roman\Roman2"
)

$Images = Get-ChildItem -Recurse | where{$_.Extension -match "gif|jpg|jpeg|png"}

If ($ListFiles-eq $True)
{
$Images
}
Else
{
# Get the computer name.
$ComputerName = gc env:ComputerName

# Create the name of the folder to copy data to.
[string]$FolderName = $ComputerName + ((Get-Date).Year).ToString() + `
((
Get-Date).month).ToString() + ((Get-Date).Day).ToString()

# Create new folder in Destingation.
New-Item -type Directory -Path $Destination -Name $FolderName

# Copy Image to the destination
$Images | Copy-Item -Destination "$Destination\$FolderName" -Force
}


}
# End: Function Copy-ImageFiles

Copy-ImageFile

No comments: