Setting a path to the user running the script?

Pascal St-Pierre 21 Reputation points
2021-06-14T20:10:25.45+00:00

I have a PowerShell script that I am running to do some updates to some distribution lists. Right now in my script I have it set to output the .CSV files to the following folder:

c:\user\stpierrepa\documents

I am looking to have this path change based on the user that is running the script, so if I use my admin account the path would automatically switch to the following:

c:\users\adminstpierrepa\documents

I have tried using the %userprofile% but it gives me an error, and have looked up different variants so far without much luck. So I am reaching out to the community for some pointers on how to do this.

Thank you in advance for your help.

Pascal

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,507 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 107.9K Reputation points MVP
    2021-06-14T20:25:04.517+00:00

    Hi @Pascal St-Pierre ,

    pleas try this:

    $filepath = "C:\Users\$Env:USERNAME\Documents"  
    cd $filepath  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.