Need some powershell help

GT98

[H]ard|Gawd
Joined
Aug 29, 2001
Messages
1,267
I'm looking to create a powershell script that will force copy a file to multiple folders.

I can get the file to copy over to the folder, but I'm struggling with the commands to make that file get copied over to subfolders.

For example:

File (call it tempfile.accdb) gets copied to C:\dbtemp\

So tempfile.accdb needs to get moved to C:\program\user 1, user 2, user 3, etc...The user folders aren't static and change, so i need it be able to just move it to the directory subfolders.
 
Based on your post, I assumed the following:

1 - The source file resides in a static location (c:\dbtemp)
2 - the destination folder(s) change (C:\ProgramUser1, C:\ProgramUser2, C:\ProgramUser3, etc.)

What I would do is:

1 - Get Child item on the folder names under "C:\Program" and store them to something like $dest (See example below, which uses $Pics instead of $dest)
2 - Iterate through the list using "copy-item" from the source location to each of the destination locations using a foreach loop

Below is an example program that illustrates using Get-ChildItem to store a list of items which are then used in a Foreach statement to run a command on each item stored in the list.

$strDRV = Read-Host "Enter Drive or Path: (Ex. C:\ or \\UNC\pathname\to\share\"
$strSize = Read-Host "Convert JPGs over this size: (Ex. 500KB or 2Mb)"
Set-Location "$strDRV"
$pics=(Get-ChildItem -rec | where {$_.Extension -eq ".jpg" -and $_.Length -gt $strSize}).fullname
ForEach ($pic in $pics)
{

c:\IV436\i_view32.exe $pic /jpgq=80 /convert=$pic
# Write-Host $pic
}

This allows you to run the script against whichever folders are present when the script is run.

You'll have to change the Get-ChildItem (GCI) statement to specify container instead of the files in it, but it's a simple command parameter...maybe with something like {$_.PSIsContainer -eq "True"}
 
Last edited:
Found a bit easier way to do it :)

dir C:\\dbtemp\* | foreach-object { copy-item -path C:\program\tempfile.accdb -destination $_ -force }
 
Shit looks like my script is fucked

dir C:\\dbtemp\* | foreach-object { copy-item -path C:\program\tempfile.accdb -destination $_ -force }

When I run it, it appends EVERY file in the dbtemp\* directory. So basically a 1kb txt file becomes a 57MB file (which is the size of the accdb file)

Any suggestions?
 
I know this is old as hell, but I think you just need -Directory switch:


dir C:\\dbtemp\* -Directory | foreach-object { copy-item -path C:\program\tempfile.accdb -destination $_ -force }
 
or better yet don't use powershell and use python but if you must wilson's solution is best
 
Back
Top