Need assistance copying task - powershell

I have .dll and .sys files under folder xyz
xyz
'QcXhciFilter8086\QcXhciFilter8086.sys'
'qSarMgr8086\qSarMgr.dll'
'qcwlan_wpextwapi8086\WapiIhvExt.dll'
These need to be copied something like this under new folder
new
'QcXhciFilter8086\QcXhciFilter8086.sys'
'qSarMgr8086\qSarMgr.dll'
'qcwlan_wpextwapi8086\WapiIhvExt.dll'
What i have tried:
'Copy-Item -Path $file_path\..\*sys -Destination C:\Users\Path\new\'
Here '$file_path = \xyz\QcXhciFilter8086\QcXhciFilter8086.sys'
Result: Only .sys.dll files getting copied directly under "new" folder. However i want them under the driver name. Something like this 'new\QcXhciFilter8086\QcXhciFilter8086.sys'

Okay, I think I understand what you are asking for...
You have a folder structure like this:
[xyz]
QcXhciFilter8086
QcXhciFilter8086.sys
qSarMgr8086
qSarMgr.dll
qcwlan_wpextwapi8086
WapiIhvExt.dll
And you want this copying to another folder. Using this will copy the folder structure and files from the xyz folder to the new folder. This will copy everything, however. If you only want to copy the files that are only .sys and .dll then you will need to add another Get-ChildItem in the existing ForeEach-Object loop to look at the current directory.
Here is my proposed solution:
$source = "c:\path\to\xyz"
$destination = "c:\path\to\new"
Get-ChildItem -Path $source -Directory | ForEach-Object{
Copy-Item $_.FullName -Destination $destination -Recurse -Container
}

Related

PowerShell to move subitems of multiple folders into their own subfolders

I'm working on a project cleaning up a file server's folder redirection folders. Would like to ask for some help with a PS script that would move the files in user folders into new "Documents" sub-folders, as we're facing manually performing this for a lot of profiles.
A tree example currently looks like:
―FolderRedirection
―――JContoso
――――――File.docx
――――――File.pptx
―――MDerby
――――――File.docx
――――――File.pptx
I'd like to be able to achieve:
―FolderRedirection
―――JContoso
――――――Documents
―――――――――File.docx
―――――――――File.pptx
―――MDerby
――――――Documents
―――――――――File.docx
―――――――――File.pptx
This should work, bear in mind for future questions you should provide at least a minimal attempt at solving the problem.
The inline comments should help you with the code logic.
# get all folders in `FolderRedirection`
foreach($dir in Get-ChildItem .\FolderRedirection -Directory) {
# create a new `Documents` folder in each sub folder
$dest = $dir | Join-Path -ChildPath Documents
$dest = New-Item $dest -ItemType Directory -Force
# get all files in each folder and move them to the new folder
$dir | Get-ChildItem -File | Move-Item -Destination $dest
}

PowerShell copy-item folder structure is NOT wanted

The folder named "z:\original" has hundreds of sub-folders containing multiple copies of the same .jpg files. I wanted to copy all .jpg files into a folder named "z:\dump" WITHOUT the folder structure and hopefully overwrite most of the copies. I used
copy-Item -path "Z:\original" -filter "*.jpg" -Destination "Z:\dump" -recurse -verbose
but this recreated the structure with the .jpg files. How can I dump all files into a single folder, using PowerShell?
Use combination of dir/Get-ChildItem and Copy-Item with pipeline.
$_.FullName - Full path to jpg file
$_.Name is file name only
Get-ChildItem Z:\original\*.jpg -Recurse | %{Copy-Item $_.FullName "Z:\dump\$($_.Name)"}
You might need to add -Force to overwrite same files

How to copy folders to specific folder in powershell?

I want to copy a folder which contain many file and other folders to a specific folder. However I get fail by using the following code.
Copy-Item "c:\F1" "c:\Fs\F1"
or
Copy-Item "c:\F1\*" "c:\Fs\F1"
The structure of folder1:
F1
F1-1
file B
F1-2
file C
file D
file A
You are probably missing the -recurse option.
Copy-Item -Path C:\F1 -Destination c:\FsF1 –Recurse

Create zip of folder and then delete the contents

I have a simple folder structure that I need to zip up to upload to AWS Lambda function
node-modules/
index.js
package.json
The files above are in the root directory. Basically the end goal is I would like a zip of all of these files and sub-files/directories instead of the structure as it is.
When I try to run the command below it says it can't access E://apps/myapp/release.zip because it's being used by another process. However I can see it starts to create release.zip but it does not have all the contents.
Add-Type -Assembly "System.IO.Compression.FileSystem";
[System.IO.Compression.ZipFile]::CreateFromDirectory(E://apps/myapp, E://apps/myapp/release.zip);
So I tried another approach. Take the folder and two files and copy them into a temporary folder, then try to zip it back into the root.
Copy-Item E://apps/myapp E://apps/myapp/temp -recurse
I do see a temp/ folder but within the temp folder it's like an inception of never ending copies until the file path gets too long.
Any tips would be much appreciated.
Issue could be that you are creating the zip file in the same folder that you are trying to compress. And you basically do the same thing when you tried using the temporary folder, hence the inception.
Try creating destination outside of the source folder being compressed.
$source = "E://apps/myapp"
$destination = "E://apps/myapp.release.zip"
# If the archive already exists, an IOException exception is thrown.
if(Test-Path $destination) {
Remove-Item -Path $destination -Force -Recurse -Confirm:$false
}
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
[System.IO.Compression.ZipFile]::CreateFromDirectory($source, $destination)
# Once archive created delete folder
if(Test-Path $destination) {
Remove-Item -Path $source -Force -Recurse -Confirm:$false
Write-Host "directory removed: $source"
}

powershell: Copy files of type AND their path values

I need to copy all my fileserver psts to another location with their folder paths.
I did the following
Get-ChildItem D: -recurse -include *.pst | copy-item -destination z:\
But this resulted in copies of like filenames being created.
I need the copy to write out the path name (its home folders so the pathname will help with easy ownership)
Any suggestions?
You could use the -recurse and -force flag on copy-item. It might create the folder structure. If it doesn't, you can use New-item to create that structure with a bit of work. If you're looking for an answer like that (i.e. you want folder structure copied as well) update your question with more detail and you'll get an answer focused on that.
What I would do, if I was just looking for a unique name for each PST, is to convert the full name to a file name on Z:, like so:
$psts = Get-ChildItem D:\ -recurse -include "*.pst"
$psts | % {
Copy-Item $_.FullName -Destination ($_.FullName.Replace("D:\","Z:\").Replace("\","-"))
}
It might be messy, but it should be unique.