PowerShell to find and copy a file with wildcard

Occasional Visitor


I need to copy a file which is located under a dynamically created subdirectory.

Directory: C:\Users\StandardUser\AppData\Roaming\Mozilla\Firefox\Profiles\dynamically-created-directory\myDesiredFile.sqlite

$SourceDir ="C:\Users\StandardUser\AppData\Roaming\Mozilla\Firefox\Profiles"
$DestinationDir = "C:\FireFox\"

# Wildecard for filter
$FileNameAndExtension = "places.sqlite"

# Copy File
Copy-Item $SourceDir $DestinationDir -Filter $FileNameAndExtension -Recurse

Problem: using the above PS settings, I am getting whole *PROFILE* ddirectory but I want to copy only file as defined in FileNameAndExtension variable ... possible?




2 Replies



You can use the following command

You can use Join-Path to build the path and include the random folder

$x=join-path ('C:\Users\Faris\AppData\Roaming\Mozilla\Firefox\Profiles') -ChildPath (Get-childItem -Path 'C:\Users\Faris\AppData\Roaming\Mozilla\Firefox\Profiles')[0]

Please note that this will fetch the first directory, if there are multiple directories then you need to build small criteria to define which one to retrieve.




If this answer helped, please click on Best Response and also a Like



@_OSD_  I would try something like the code below, assuming there are multiple profile directories and you want to replicate that in the destination directory.


$SourceDir = 'C:\Users\StandardUser\AppData\Roaming\Mozilla\Firefox\Profiles'
$DestinationDir = 'C:\FireFox\'
$FileNameAndExtension = 'places.sqlite'

$SQLiteFiles = Get-ChildItem -Path "$SourceDir*\$FileNameAndExtension"

foreach($SQLiteFile in $SQLiteFiles){
    $ParentFolder = $SQLiteFile.Directory -replace [regex]::Escape("$SourceDir")

    New-Item -ItemType Directory -Name "$DestinationDir$ParentFolder"
    Copy-Item -Path $SQLiteFile -Destination "$DestinationDir$ParentFolder"



Hopefullly I have understood what you were after. ;)