Question:
Can anyone explain why using something like:
1 2 |
gci -force "\\computername\c$\users\username\Documents" -recurse |
or
1 2 |
gci -force "\\computername\c$\users\username\My Documents" -recurse |
When looking at the output it is actually returning:
1 2 3 4 |
"\\computername\c$\users\username\My Documents\" "\\computername\c$\users\username\My Documents\My Pictures" "\\computername\c$\users\username\My Documents\My Music" |
Etc?
The “My Music” and “My Pictures” are not under that location, but “Documents” or “My Documents” seems to report all of these folders.
However, if I use ‘dir’ it ONLY returns the files that are physically under ‘My Documents’. Also if I gci ‘My Pictures’ it ONLY returns the contents of ‘My Pictures’ I know the various profile folders are special folders but is there a way to JUST return the contents of ‘My Documents’.
My script is basically parsing the user profiles and returning the size of all machines in preparation for redirected profiles.
So the output is always something like:
1 2 3 4 5 6 7 |
Scanning users ........... testuser .......................... My Documents => 500 MB .......................... My Pictures => 500 MB .......................... My Videos => 0 MB .......................... Downloads => 0 MB .......................... Total => 1000 MB |
So in the example the ‘Total’ is actually just reporting the 500 MB of pictures, one in ‘My Pictures’ and once in ‘My Documents’
Answer:
This is not powershell specific, this is because Windows 7 has Junctions in any profile under the Documents folder for My Music, My Pictures, and My Videos. This can be seen by pulling up a command prompt, navigating to your Documents folder, and doing a DIR /AD
which will show you those junctions.
To avoid this problem within PowerShell do not use the -Force option, and it should not include junctions.
This behavior can be seen by doing the same within PowerShell as well if you would prefer, by performing GCI $env:UserProfile\Documents
and noting there is no listing for the three junctioned folders, and then doing the same but appending -Force
to it and noting that those three now appear.
As pointed out by Rob Prouse, this only works if the user’s Documents folder is located in the default location. If it has been redirected somewhere else then it will not get you that redirected location. It works for the OP’s original issue, since they were obviously looking at the default location, but for other users you will want to get the path using [environment]::GetFolderPath('MyDocuments')