Copy-Item cmdlet should only copy difference - powershell

Im writeing a backupscript useing powershell. I know that i could just use robocopy or rsync but i would like to do this in powershell. The problem i have has to do with the copy-item cmdlet. What my script does:
Read var's and fills them from a csv
Pings destination host
Checks if Outlook is open on source host and asks if it should close it
Then it should copy some folders onto the destination
My problem is that it always does a full copy of all files. I would like it to only copy the files that were changed or do not exist on the destination.
The second problem i have is that in Win7 there are hidden systemfolders in "Users/Documents" that link to "My Pictures" and "My Videos". I dont need to copy these but i didnt manage to exclude them useing the exclude agrument.

Just some quick and general suggestion...
I would like it to only copy the files that were changed
I would copy the files comparing the source and destination modified date
The second problem i have is that in Win7 there are hidden systemfolders in "Users/Documents" that link to "My Pictures" and "My Videos". I dont need to copy these but i didnt manage to exclude them useing the exclude agrument. :
Do not use copy-item directly, but use the output of get-childitem without force parameter. This will prevent to copy hidden or system files.

Related

powershell symlink from online location

I have business laptop which I can use off hours for entertainment.
Unfortunately most of my personal folders ale located on the server and that causes some issues.
My 'Documents' folder is located on:
\\<server name>\RedirectedFolders$\<username>\Documents
Which causes me problems with game that want to use 'My Games' folder located inside 'Documents' folder.
I tried to move 'My Games' to other folder located locally on disk C:, then make symbolic link with PowerShell using command:
New-Item -ItemType SymbolicLink -Path "\\<servername>\RedirectedFolders$\<username>\Documents\My Games" -Target "C:\var\games\My Games"
Unfortunately it gives error:
New-Item: This operation is supported only when you are connected to the server.
Does anyone knows how to solve that?
EDIT. Forgot to mention, I'm connected via VPN to my company so when I type in explorer my online link to 'Documents' folder it opens it normally.
You reversed the arguments. You want the -Target to be the real directory path and the -Path to be the local directory link you access the target at. You can't create a symbolic link on a remote share to a local target.
Both methods work for me:
New-Item -ItemType SymbolicLink -Target \\server.domain.tld\share\SomeFolder -Path .\LocalSomeFolder
cmd /c mklink /D .\SomeLocalFolder \\server.domain.tld\share\SomeFolder
That said, this won't work like mapped drives and offline files. If you aren't online, you aren't accessing any of those folder's files. You can work around this by mapping the share to a network drive and ensuring offline file access is enabled for the mapping, then symlinking somewhere within the letter drive for the target rather than at the full network path.
This does bring its own share of issues, however, as "Offline Files" for network drives can be finicky.
Workaround
You can work around this gracefully by changing the location of your Documents library to %USERPROFILE%\Documents. Note that this won't migrate your existing files so it's up to you to make sure you can obtain the files before or after re-mapping the Documents library and populating your local Documents library with your expected files. Keep in mind some organizations will block changing some or all library locations so this might not be possible for you to achieve on a company-managed workstation.
Also note that this will no longer keep a remote copy of your local Documents library, which might interfere with any backup processes your organization has in place for your workstation. It becomes up to you to keep your Documents backed up in this case.

How do I output the files being copied in console?

I'm writing a PowerShell code to copy files in a folder to another folder. I want the console to display the files that are being copied, as well as the file size if possible.
Currently, I have tried using -Verbose But the output is not very readable.
I would like the console to display the files being copied, and the file size.
You can use the parameter -PassThru for Copy-Item. But it will not show you the file size.
I would recommend you to use robocopy.exe for any copy jobs in powershell. It is more reilable and in your case it will show you the filesize.

How to run a powershell script at active directory login

I created a group policy in In Group Policy Management Editor, in the navigation pane, expand User Configuration, expand Policies, expand Windows Settings, and then click Scripts (Logon/Logoff). I made a logon script as a ps1 file:
copy-item "\\server1\Pictures\background.jpg" -Destination "C:\screensaver\" -Recurse
I added that ps1 file in the powershell scripts part of the group policy and set it to run powershell scripts first.
I didn't use any parameters which may be causing the issue?
I need each computer to have that c:\screensaver\background.jpg image when they login.
It's the only group policy applied to that OU, all the PCs are Windows 10, and the domain controllers are Windows 2012 r2.
In my opinion creating a (PowerShell-) logon-script for copying a file is not a great solution and out-of-date nowadays.
Make your life easier and use group-policy-preferences for this task. You don't have to create scripts for that.
Open the Group Policy Management Console, select your policy, open the "Preferences"-Node and select "Files". Create a new element and select the source- and the target (as shown below).
After that reboot the client and the file should get copied without coding.
Sounds like there's two parts to implementing your request. Before doing any of the following, make sure that you can log in as one of the users, and manually perform the steps you want the script to complete (to make sure any user restrictions aren't holding you up). So, make sure you can navigate to the remove image location \\server1\Pictures\background.jpg, and copy it to the local folder C:\screensaver.
Copying the file to the target machine. You provided the contents of your PS1 file as copy-item "\server1\Pictures\background.jpg" -Destination "C:\screensaver\" -Recurse. I believe you'll want to have two slashes "\\" at the beginning of your \server1 string, resulting in "\\server1\Pictures\background.jpg" (the two slashes make this a valid UNC path). Additionally, you included the -Recurse parameter. I don't understand the need for this parameter, based off of the documentation, accessible via the command Get-Help Copy-Item -Full.
-Recurse [<SwitchParameters>]
Indicates that this cmdlet performs a recursive copy.
I would suggest that you include the -Force parameter. If you ever update that image, the next time a user logs on, they'll receive the updated image. Without the -Force parameter, the command might not overwrite the existing image on disk. Also, you shouldn't need the trailing slash on the -Destination parameter. I would suggest the command resemble:
Copy-Item "\\server1\Pictures\background.jpg" -Destination "C:\screensaver\" -Force
Configuring the wallpaper via Group Policy The first link I found via Google Search set windows 10 wallpaper via group policy with anything that looked like useful steps was some grouppolicy.biz website. I haven't been able to test them, but the main point being that you'll need to make sure that you're actually telling the computer to use the wallpaper you've copied into place.
If you make sure that you've addressed the above items, then it should work for you. There may be some considerations for the first time a user logs in, if the image isn't copied over, then the wallpaper may not display until the second time they log in.

Powershell Copy-Item - Access Denied

I'm writing a script that will open files in a directory and perform a find/replace. But before I do that I would like to make copies of the files in the same directory as the original file. All files exist in the C:\Program Files (x86) directory. The script errors out stating that access to the directory is denied. I see that Copy-Item has a -Credential parameter, but on my test machine I don't have any local Administrator permissions. Doesn't seem like there is going to be any way to copy the file to the same directory. Can I specify the system's temp folder (Windows 7 Professional) and just write it there? I'd like to find a way to copy the file to the same directory as the source file, however.
Windows Vista and above default to not allowing non-administrative users to write to the `%PROGRAMFILES% folder. This means that you're not going to be allowed to copy the files there; you're also not going to be able to save them after doing your find/replace operation.
You can write them to your user documents folder (%USERPROFILE%\Documents) folder instead, if that will work for you.

Powershell: How to copy a file from TFS to a different destination

I'm writing a powershell script for deployment. I need to copy changed files from TFS to our Test Server. I have been able to retrieve the change sets, and I have been able to drill down to the Item. I have access to the path of the source file.
Does anyone know an efficiect way of doing this? Do I need to use the DownloadFile cmdlet or can I just use the Copy-Item cmdlet.
path of sourcefile is $file.ServerItem which resolves to, for example, $/Project/PromonetBaseline/Main/Source/ItemHierarchy.vb
Destination is a path like \\104Server\WebApps\PromonetBaseline\Main\Source\ItemHierarchy.vb
Is there a neat way to do this programatically?
Any input is appreciated.
Thanks,
Akin
For something like this, I would set up a local workfold mapping for the source files, get those files and then use Copy-Item to copy the source files to the destination folder. You can use the -Force parameter on Copy-Item to overwrite an existing file.
Another option is to use tf view itemspec /i > tempfilename to get the files from the server without creating a local workfold mapping.