powershell symlink from online location - powershell

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.

Related

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 "."

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.

Get-ChildItem is not showing all files in a folder

When I call Get-ChildItem in PowerShell it is only returning a few of the files that exist in the directory. This is the driver folder, so I tried using the -Force parameter in case they were hidden, but with no luck.
It's interesting though because it works perfect on my Windows 7 32 bit, but not 64 bit. Any ideas?
I believe PowerShell is showing you everything however the folder you're looking at in the x86 PowerShell prompt isn't what you think. The directory you're actually looking at is under C:\Windows\SysWow64\Drivers and not actually C:\Windows\System32\Drivers. This is due to a Windows feature (Vista and higher) for 32-bit processes running on 64-bit OS called virtualization (specifically the File System Redirector). When you run a 64-bit PowerShell prompt virtualization is not used so you see the real C:\Windows\System32\Drives dir.
From a 32-bit PowerShell prompt, you can see the "real" C:\windows\system32\drivers dir by using this path:
Get-ChildItem C:\Windows\SysNative\Drivers
I ran across this while searching for a similar issue. I want to list user folder usage, but run into issues with folder ownership/permissions. Even though I am a local admin, I need to explicitly give myself access. Not ideal. Below gives accurate usage, despite permissions. (Of course, if you want to know more detailed usage, you need to use a for loop or something.)
Get-ChildItem "C:\Users" -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object Length -Sum
As for what files are included, hidden and system files are not shown by default: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-6
Hidden parameter: Hidden files only
System parameter: System files only
Force parameter: Include hidden and system files, with regular files
To confirm, I granted myself permission to one of the user directories. I compared size reported from PowerShell (before granting permission) and that reported in File Explorer (after granting permission). Size and count was the same.

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.