Friday, September 23, 2016

PowerShell Copy and Rename Files


# Define variables.
$Source = "C:\Test\*.*"
$Destination = "C:\Testing\"
$DestinationFiles = "C:\Testing\*.*"

# Create the $sourceFileList variable to loop through
$sourceFileList = Get-ChildItem -path $Source

# Loop through the $soureFileList and copy the items to the $Destination.
foreach ($item in $sourceFileList)
{
    Copy-Item -Path $Source -Destination $Destination
}

# Create the $destinationFileList variable to loop through
$destinationFileList = Get-ChildItem -path $DestinationFiles

# Loop through the $destinationFileList and rename the files with appended DateTime stamp.
foreach ($itemDest in $destinationFileList)
{
    $Date = (Get-Date).ToString("yyyyMMdd_HHmmss")
    $newFileName = $Date + "_" + $itemDest.Name
    Rename-Item $itemDest -NewName $newFileName
}

No comments:

Post a Comment