Powershell Copy-Item - Access Denied - powershell

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.

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.

Compersing a new file in a backup folder and move it to another folder

On a Windows 10 machine, I have a backup folder where I have daily db.bak files coming from an automated SQL server backup job.
I am trying to figure out a way to create a duplicate zip file for every new .bak file that comes to the backup folder and move that zip file to a new location without deleting the original .bak file.
I am thinking this is possible with PowerShell, but have not figured out a way to do this as an automated process.
This is all you need to do:
Compress-Archive -Path "D:\Test.bak" -DestinationPath "F:\Backup\Test.zip"

Powershell - Unable to create directory in Program Files and specify install path

I am trying to write a simple script but I am running into trouble when I try to create a directory in "C:\Program Files".
New-Item -Path "C:\Program Files\JHA" -ItemType Directory
When I do this, no directory is created. No error is thrown. I have admin privileges on my computer. Any advice?
Also, in this script I would like to run an installer and specify this path I created above as the directory it installs to. Is that possible? I read one post where the person was using IMAGEDIR= but someone said Powershell doesn't allow you to specify the installation folder.
Thanks for any help.

Export-Csv - Access to the path 'C:\export.csv' is denied

I'm getting the information I need out of the line, although when I go to export the information, I get an error.
When I run this code:
get-process | Export-csv -Path "C:\export.csv" -NoTypeInformation
I get this error:
Export-csv : Access to the path 'C:\export.csv' is denied.
At line:1 char:14
+ get-process | Export-csv -Path "C:\export.csv" -NoTypeInformation
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
How can I export to CSV?
You need to use an admin console or change the location of the csv file to C:\temp or something. The home drive require admin permissions.
EDIT:
The elevation requirement is only with system directories like:
C:\
C:\Windows
C:\Program Files
C:\Program Files (x86)
C:\Users
Notice that you can change C:\Users\<YourUserName> but not any other directories under C:\Users. Windows was built for multiple users even if you may be the only one using it. The above directories are common for all users and changing its contents would effectively change the Windows environment for other users too since windows rely on the integrity of these directories. Allowing a normal user to delete/modify/add files to these could have dangerous results for the OS and other people who use it.
So it tries to makes sure that only an Admin user is authorized to make changes to it. It is also the same reason why you get prompted for admin permissions when installing a software. It is installed to C:\Program Files or C:\Program Files (x86), make changes to the registry and is available to all users of that computer. Same with removing it. If a program does not require to do that, it doesn't require admin privileges. Eg: Portable softwares. Some programs only use the user directory or users temp directory C:\Users\<Username>\AppData\Local\Temp. Such programs are only available to the user that installed it.
If you have a custom directory like C:\myGames, adding files to it wont require elevation. So C:\Temp is just another directory like that. You will also notice that C:\ will let a normal user create a folder but not a file.
SUMMARY: Any changes that affect other user directories or the windows environment will require admin rights. Others don't.
In my case, the error occurred because I failed to specify the file name. The correct syntax is;
Export-Csv -path ".\targetList.csv"
and not -path "."

Copy-Item cmdlet should only copy difference

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.