Ying Li at myITforum.com

PowerShell & System Center

Powershell Script to find and replace a file on a computer

This script will find a file on C drive (or whatever drive we choose), rename it and replace it with another file. The new file should be in the same directory where you execute your powershell script.


#Because of CIM_DataFile, split File Name and Extension into two variables
$strFileName = "Foo"
$strFileExt = "doc"
$strNewFileName = "Foo2.doc"

$colFiles = get-wmiobject -query "Select * from CIM_Datafile where FileName = '$strFileName'and extension = '$strFileExt' and drive ='C:'"  
Foreach ($objfile in $colfiles)
{
Ren $objfile.name -newname ($objfile.filename +".bak")
copy-item $strNewFileName -destination ($objfile.drives + $objfile.path)
}

Posted: Dec 13 2006, 02:08 PM by yli628 | with 1 comment(s)
Filed under:

Comments

xp find my computer executable said:

Pingback from  xp find my computer executable

# May 31, 2008 7:47 AM