How do I pass option flags to Folder.CopyHere in PowerShell? How do I pass option flags to Folder.CopyHere in PowerShell? powershell powershell

How do I pass option flags to Folder.CopyHere in PowerShell?


You can use 4 -bor 16. It is hard to tell what this method expects since the type is VARIANT. I would have thought that it would take an integer value. If that doesn't work, this comment from the MSDN topic on Folder.CopyHere implies that a string should work:

function CopyFileProgress{    param( $Source, $DstFolder, $CopyType = 0 )    # Convert the decimal to hex    $copyFlag = [String]::Format("{0:x}", $CopyType)    $objShell = New-Object -ComObject "Shell.Application"    $objFolder = $objShell.NameSpace($DestLocation)     $objFolder.CopyHere($Source, $copyFlag)}

Although I wonder if the format string should be "0x{0:x}"?

Just be aware that for normal .NET flags style enums, you can pass multiple flags to a .NET (or command parameter) that is strongly typed to the enum like so:

$srv.ReplicationServer.Script('Creation,SomeOtherValue')

Oisin has written up some info on this subject in this blog post.


I had the same problem and found this in another thread, Worked perfectly for me.

If you want it to overwrite AND be silent change 0x10 to 0x14 (docs).

$destinationFolder.CopyHere($zipPackage.Items(), 0x14)


The Folder.CopyHere option flags may simply not work. This makes me sad. I'll have to investigate one of these other methods, all of which leave me in a bit of a bind.

Separate Process

Invoke the copy in a new process and hide the window using the ProcessStartInfo properties. I haven't implemented this yet, but I wonder if it will address the user-prompting for overwriting existing files?

Dim iProcess As New System.Diagnostics.ProcessStartInfo(AppDomain.CurrentDomain.BaseDirectory + “unzip.exe”)iProcess.CreateNoWindow = TrueDim sArgs As String = ZippedFileiProcess.Arguments = sArgsiProcess.WindowStyle = ProcessWindowStyle.HiddenDim p As New System.Diagnostics.ProcessiProcess.UseShellExecute = Falsep = System.Diagnostics.Process.Start(iProcess)p.WaitForExit(30000)Dim s As Integer = p.ExitCodeiProcess.UseShellExecute = Truep.Dispose()iProcess = Nothing

For Loop

Only copy non-existing items. This seems to fall down when I actually want to update an existing font with a new font file of the same name.

foreach($File in $Fontdir) {    $fontName = $File.Name.Replace(".ttf", " Regular")    $objFolderItem = $objFolder.ParseName($fontName);    if (!$objFolderItem) {      $objFolder.CopyHere($File.fullname,0x14)    }}

Remove Existing

I'm thinking of removing all fonts of the same name as the ones I'm copying, then copying the set. Although that's kind of brutal. And I believe that there's another prompt if that font cannot be deleted because it's in use. sigh