Windows Right-Click copy vs Powershell Copy-Item - windows

I want to copy a directory containing log files, but my script is timing out when using powershell to perform the copy. If I simply right-click the log directory and copy it, it works fine.
Copy-Item -Path "$logpath" -Destination "$archivepath" -Recurse
The log directory is actively being written to which is what is causing the time-out. How can I copy this directory with powershell? I can copy it using Right-click but I can't with powershell.

Using Powershell v5
$logpath="C:\Path\To\LogDir"
$backuppath="C:\Path\To\BackupDir"
Set-Clipboard -Path "$logpath"
Get-Clipboard -Format FileDropList | Copy-Item -Destination "$backuppath" -Recurse
Using Robocopy
Robocopy.exe "$logpath" "$backuppath" /E | Out-Null

Related

Need script to move folders based on certain file types in the folder

I have a scenario where I need to search folders and sub folders for certain file types. if those types are found then the whole root folder with all files and sub folders needs to be moved.
Source c:\testsource
example: search for *.exe files
the following file is found
c:\testsource\folder1\subfolder1\testfile.exe
then folder1 with all files and subfolders needs to be moved to c:\testdestination
I first tried this with a batch file and was able to move all files in subfolder1 but not any of the other files or directory structure. I then started working on it in powershell but had similar results. I think what I need is to search, and if found capture the folder path, then move the folder, but not sure how to do that.
Batch file I created:
for /r "C:\TestSource" %i in (*.exe)do move "%~dpi\*" "C:\TestDestination\"
Powershell Script
$Source = "C:\testsource"
$Dest = "C:\testdestination"
Get-ChildItem -Recurse -Path $Source | Where {$_.fullname -Match '.*\.exe'} | Move-Item -Destination $Dest
Any help here would be much appreciated
You will need a loop to check for each direct subfolder of the source folder if it has the desired file in one of its subfolders. ... something like this:
$Source = 'C:\testsource'
$Dest = 'C:\testdestination'
Get-ChildItem -Path $Source -Directory |
ForEach-Object{
If (Get-ChildItem -Path $_.FullName -Recurse -File -Filter '*.exe'){
Move-Item -Path $_.FullName -Destination $Dest -Recurse
}
}

Execute PS from bat for Intune Win32

I am so confused and actually also not so familiar with PS and bat after my troubles with them.
I want to set the Lockscreen in Windows 10 with Intune through an IntuneWin file (WIN32 application).
I have a folder with the image, which I want to set, a copy.bat which should copy the image in the directory and also execute the PS file for setting the login image and a del.bat for deleting the image.
copy.bat
md %AllUsersProfile%\sz
copy /Y Wallpaper.jpg %AllUsersProfile%\sz
powershell -ExecutionPolicy Bypass -File Set-Lockscreen.ps1 -verb RunAs
del.bat
del /Y %AllUsersProfile%\sz\Wallpaper.jpg
Set-Lockscreen.ps1
$RegKeyPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\PersonalizationLS"
$LockScreenPath = "LockScreenImagePath"
$LockScreenStatus = "LockScreenImageStatus"
$LockScreenUrl = "LockScreenImageUrl"
$StatusValue = "1"
$path = "C:\ProgramData\Elinvar"
$LockScreenImageValue = "C:\ProgramData\sz\Wallpaper.jpg"
sIf ((Test-Path -Path $path) -eq $false)
{
New-Item -Path $path -ItemType directory
}
if (!(Test-Path $RegKeyPath))
{
Write-Host "Creating registry path $($RegKeyPath)."
New-Item -Path $RegKeyPath -Force | Out-Null
}
New-ItemProperty -Path $RegKeyPath -Name $LockScreenStatus -Value $StatusValue -PropertyType DWORD -Force | Out-Null
New-ItemProperty -Path $RegKeyPath -Name $LockScreenPath -Value $LockScreenImageValue -PropertyType STRING -Force | Out-Null
New-ItemProperty -Path $RegKeyPath -Name $LockScreenUrl -Value $LockScreenImageValue -PropertyType STRING -Force | Out-Null
RUNDLL32.EXE USER32.DLL, UpdatePerUserSystemParameters 1, True
I pack all these files with Microsoft Win32 Content Prep Tool and upload this in Intune as Win32 file.
Upload works, assigning to a group works, installation is successful. All good so far, I thought.
If I check the directory, the image is in %AllUsersProfile%\sz.
But when I check the reg, the entry is not set.
When I run the copy.bat file manually, It doesn't work. Only when I run it as administrator.
When I run the last line in cmd as administrator, so
powershell -ExecutionPolicy Bypass -File Set-Lockscreen.ps1 -verb RunAs
It works as well.
I think Intune is not running the script as administrator.
In Intune there is no configuration to say, run this command as administrator.
Maybe with a syntax? Does anyone know this?
Something like
copy.bat RunAs
I also export the reg file and import this with
reg import PersonalizationLS.reg
It didn't work.
I think there must be a way to execute the installation command in intune to run the script as admin.
It needs just for the last line the administrator privilege , md and copy work without administrator privileges. (same also for important reg file).

Copy all .jpg from subfolders to a single folder by cmd

I have a folder that has 148 folders in it, and in each of these folders they have a .jpg file. I need to get all these .jpg and put in a single folder
each folder is at least 1 .jpg
I can do this on Linux as well
`#!/bin/bash
for file in `find source -name * .jpg`;
of the mv "$ file" Destination;
done;
but I can not play this in Windows. Only the find command that I can reproduce the same result: dir /S /B *.jpg
for /r %f in (*.jpg) do move %f %destination%
Note: This is the interactive version. In a command script, you need to protect the %f's from too-early variable substitution.
for /r %%f in (*.jpg) do move %%f %destination%
Another way to do it using PowerShell. When you see that the files are being moved as you expect, remove the WhatIf from the Move-Item cmdlet.
Get-ChildItem -Recurse -File -Filter '*.jpg' |
ForEach-Object { Move-Item -Path $_.FullName -Destination 'C:\the\other\dir' -WhatIf }
You can run this in a cmd.exe shell or .bat script.
powershell -NoLogo -NoProfile -Command ^
"Get-ChildItem -Recurse -File -Filter '*.jpg' |" ^
"ForEach-Object { Move-Item -Path $_.FullName -Destination 'C:\the\other\dir' -WhatIf }"
The easiest way to achieve this is in Windows is to open to root folder with the Windows Explorer at which you want to start searching all the elements. Then go to the small input field for searching and enter *.jpg the result will show all the jpg in the root folder and all subfolders. Then you can simply copy and paste the files to your destination folder.

Powershell Command/Script to search and delete folders and sub folders

good day, i was searching all over the internet for a powershell command/script that can search and delete any folder named (Games*) (game*) on remote machines
on CMD i used to command "del /s \computername\c$\Game*" and it works perfectly with me however it delete files only, need a powershell script that can delete files and folders.
Make use of pipeline option in powershell .Below code will do the work.
get-childitem -path 'FilePath' -Recurse -Filter "Game*" | remove-item -Force
Hope it HElps.
Remove-Item \\compname1\c$\Game* -force -recurse

Copying files and folders of only batch and vbs script files over network

I am in the process migrating my servers from 2003 to 2008 and trying to write a powershell script that will copy all files that are vbs and bat from the 2003 server to the 2008 server and also keep the folder structure in tact as it is vital for the program to run.
Here is what i have so far. It works kinda. it will create the D:\folder on the new servers but it will not copy anything over. It also creates the log.txt with all of the names of the vbs and bat files and there paths.
New-PSDrive -name source -PSProvider FileSystem -root "\\servername\d$\folder" |Out-Null
$targetdirectory = "d:\folder"
$sourcedirectory = "\\servername\d$\folder"
Get-ChildItem -Path $sourcedirectory -filter "*.bat","*.vbs" -Recurse| Out-File D:\log.txt
if ( -Not (Test-Path $targetdirectory)) {New-Item -path $targetdirectory -Type Directory | out-null }
Copy-Item -Path $sourcedirectory -filter "*.bat","*.vbs" -Destination $targetdirectory -recurse -force
remove-psdrive -name source
The part that is not working is this
Copy-Item -Path $sourcedirectory -filter "*.bat","*.vbs" -Destination $targetdirectory -recurse -force
I just changed it to
Get-ChildItem -Path $sourcedirectory -Include "*.bat","*.vbs" -Recurse| Out-File D:\log.txt
if ( -Not (Test-Path $targetdirectory)) {New-Item -path $targetdirectory -Type Directory | out-null }
Copy-Item -Path $sourcedirectory -Include *.bat, *.vbs -Destination $targetdirectory -recurse -force
It would be easier to use robocopy.
robocopy source dest *.vbs *.bat /s
Reading the comments from the other answer it just looks like you are having an issue filtering certain files for use with Copy-Item yes? In most cases you will see suggestions of using Get-ChildItem to isolate the files you want and piping to Copy-Item. You are almost already doing this.
$files = Get-ChildItem -Path $sourcedirectory -filter "*.bat","*.vbs" -Recurse
$files | Out-File D:\log.txt
$files | Copy-Item -Destination $targetdirectory -Force
$files contains which files you wanted to isolate (You could pipe it into Where-Object to get files of a certain size if need be.). Take files and output the results to an external txt file (Using Export-CSV can make for cleaner output of object FYI). Then we simply pipe it into Copy-Item. No need to try and file again.

Resources