Creating a folder in the current users documents folder through powershell

Welcome Forums General PowerShell Q&A Creating a folder in the current users documents folder through powershell

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
9 months, 3 weeks ago.

  • Author
    Posts
  • #78913
    KD

    Participant
    Points: 0
    Rank: Member

    Hello,

    I am working on a script and I will need to create a folder in the current logged in users documents folder. Since the script will be used by multiple users, I will need to directory to be created in C:\Users\%UserProfile%\Documents but I'm not sure how to specify that the directory should be created in the current logged in users 'Documents' folder.. any ideas?

    Thanks

  • #78919

    Participant
    Points: 0
    Rank: Member

    You can always use the $Home value to build the directory path where you want to put the folder. The $Home variable contains the path to C:\Users\UserProfile (Ex: C:\Users\user1), for each logged in user. So, you could do something like the following to create the directory.

    $Path = "$Home\Documents"
    New-Item -Name NewFolder -Path $Path -ItemType Directory
    
    • #78922
      KD

      Participant
      Points: 0
      Rank: Member

      Exactly what I needed, thank you very much!

    • #78973

      Participant
      Points: 0
      Rank: Member

      Not a problem. Glad I could help. 🙂

  • #78920

    Participant
    Points: 0
    Rank: Member

    Part of my above explanation text got cut off, so I corrected it with text that didn't get cleared off.

  • #90590

    Participant
    Points: 0
    Rank: Member

    The approach with $Home use does not work when the Documents folder is moved to another location.
    I had a similar problem and had to use this instead:

    [environment]::GetFolderPath("mydocuments")

The topic ‘Creating a folder in the current users documents folder through powershell’ is closed to new replies.