Move all files with same prefix using Powershell

We have an application that we run on a server that generates a set of logs that all have the same prefix, for example, abc123_johnson.log, or abc123_smith.log. Every user has a corresponding log with there name preceded by the same prefix. Here is a simple way to recursively search that directory for all files with that prefix and move them.

# This script is to manage the abc123_username logs on the Server "A", to keep thirty days of logs, then
# move them off to a share on the Server "B", then delete them. (The logs take too much space on the Server "A".)
# The move cmdlet will automatically replace the old files on Server "B" so no need to delete them

# Move the logs from the last thirty days from Server "A" to Server "B"
Get-Childitem \\SERVERNAME_A\SUBFOLDER\SUBFOLDER -recurse -include abc123_* | Move-Item -force -destination \\SERVER_B\SUBFOLDER

# Logging of some kind below to indicate success.
Remove-Item -Path D:\SERVERLOGSMOVE\Serverlogsmove.txt -force
New-Item D:\SERVERLOGSMOVE\Serverlogsmove.txt -type file

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s