38

I want to use a PowerShell script to automate tasks related to the user when the user logs in.

Sometimes a user will have moved his Documents folder from the default location. How can I determine the location of the user's Documents folder in PowerShell?

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
Zulgrib
  • 609
  • 1
  • 7
  • 14
  • Your latest revision changed the question too much. The question you originally asked has been answered, please post a *new* question about looking up folder paths from a GUID. – Harry Johnston Jul 24 '14 at 04:20

3 Answers3

70

try this:

[Environment]::GetFolderPath("MyDocuments")
Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148
CB.
  • 58,865
  • 9
  • 159
  • 159
  • 1
    Hello, it works for some folders but not all, for example, i cannot retrieve system's "Saved Games" folder with this (or i'm not doing it correctly) – Zulgrib Jul 17 '14 at 16:23
  • Created another question about it here http://stackoverflow.com/questions/25049875/getting-any-special-folder-path-in-powershell-using-folder-guid – Zulgrib Jul 31 '14 at 02:27
  • This does not work:`Set-Location [Environment]::GetFolderPath("MyDocuments")` `Set-Location : Es wurde kein Positionsparameter gefunden, der das Argument "MyDocuments" akzeptiert.` However, if I create a variable as Deeps does, it works.. – Timo Aug 12 '20 at 09:34
  • 1
    @Timo You need to add parentheses around the second function call: `Set-Location ([Environment]::GetFolderPath("MyDocuments"))`. You can't just do `Do-Something -Path Get-Something -Param`, but `Do-Something -Path (Get-Something -Param)` works. – sk22 Dec 16 '20 at 09:36
  • I want to use `python` to access the `documents` windows folder of my user. I use a german windows version. Is the raw documents string to be put in english or german? Have no windows at hand to try at the moment. I need `documents = os.path.join(home, 'documents')` – Timo Jun 16 '21 at 18:30
  • I want to set an `alias/ function` using: `function pic { Set-Location -path ([Environment]::GetFolderPath("Mypictures"))}`. Result: `The term 'C:\Users\User\Pictures' is not recognized as a name of a cmdlet,..` – Timo Jun 26 '21 at 05:48
18

To get a list of known folder names use the following command:

[enum]::GetNames( [System.Environment+SpecialFolder] )

To get a list of names and paths:

[enum]::GetNames( [System.Environment+SpecialFolder] ) | 
    Select @{ n="Name"; e={$_}},
        @{ n="Path"; e={ [environment]::getfolderpath( $_ ) }}
3
$mydocuments = [environment]::getfolderpath("mydocuments")
Deeps
  • 330
  • 1
  • 5
  • 16
  • Hello, it works for some folders but not all, for example, i cannot retrieve system's "Saved Games" folder with this (or i'm not doing it correctly) – Zulgrib Jul 17 '14 at 16:22