Jon
Jon

Reputation: 3

PowerShell: Using $env:userprofile in a desktop shortcut statically between users

I have a PowerShell program that generates some number of new user accounts on the system. Within each user, I have a log folder that I create somewhere within the user account, and an associated desktop shortcut that links to that log folder. To do this, I set the shortcut's target path to something like $env:userprofile + "\LogsFolder"

The issue I'm running into is that, on the current account (the one that generates the new account and logs folders), the shortcut itself will retain the $env:userprofile variable. So when I navigate to C:\Users\NewUser\shortcut, I don't see the shortcut going to C:\Users\NewUser\LogsFolder, but instead it leads to C:\Users\CurrentUser\LogsFolder, which is entirely different.

If I log into NewUser, the shortcut will correctly navigate to C:\Users\NewUser\LogsFolder, but it leads to the wrong folder when I log back into CurrentUser. My question is, how can I store the $env:userprofile path statically when I generate these shortcuts, so that instead of $env:userprofile\LogsFolder I get C:\Users\NewUser\LogsFolder, and so the shortcut path remains static between different users?

Quick update for a better explanation: since administrator accounts can navigate to other user folders, I was hoping that an administrator account would be able to access the logs folder for each user individually, rather than having to log into the different users in order to grab their logs.

Update on the layout of the scripts: The administrator account runs the user generator script which creates the actual new user accounts, and it then has those user accounts run the shortcut/logging script that creates the new shortcuts on the desktop. I don't provide any usernames or parameters to the shortcut/logging script, and since it is run by the new user accounts, the $env:userprofile variable is used to tell the script where to put each shortcut. I wanted to avoid providing the username of each account to the script manually since it seemed like $env:userprofile would work fine, but since the shortcut itself changes between users, the solution that seems to make the most sense is to provide the account name directly to the shortcut/logging script so that it can generate the shortcuts statically. This requires modifying the overhaul structure of the shortcut/logging script however, which is the main issue and why I want the actual contents of $env:userprofile itself.

A sample of my code for reference:

$shell = New-Object -ComObject ("Wscript.shell")

# Correctly generates the shortcut on the new user/user who is running the script
$shortcut = $shell.createshortcut("$($env:USERPROFILE)\shortcut.lnk")

# Path changes depending on who is logged in
$shortcut.TargetPath = "$($env:USERPROFILE)\LogsFolder"

$shortcut.Save()

Desired behavior: $shortcut.TargetPath is statically set to "C:\Users\NewUser\LogsFolder"

Current behavior: $shortcut.TargetPath is either "C:\Users\CurrentUser\LogsFolder" or "C:\Users\NewUser\LogsFolder" depending on the current user.

Upvotes: 0

Views: 7601

Answers (1)

mklement0
mklement0

Reputation: 437428

Using an expandable (double-quoted) string ("..."), "$($env:USERPROFILE)\..." (which can be simplified to "$env:USERPROFILE\..."), instantly expands to the value of the USERPROFILE environment variable as defined at that time, and therefore uses the path for the account that generates the new user, not the one for the new user.

Therefore, you need the following:

# Create the shortcut file in the *new users*'s profile folder.
# This requires you to specify the path (ultimately) *literally, statically*.
# I'm assuming that the new user's username is stored in $newUser:
$newUserProfile = "C:\Users\$newUser"
$shell.createshortcut("$newUserProfile\shortcut.lnk")

# For a shortcut file created in a dir. specific to the new user,
# you *can* use an environment-variable reference as follows:
$shortcut.TargetPath = '%USERPROFILE%\LogsFolder'
# Alternatively, for a *static* value, use:
#  "$newUserProfile\LogsFolder"

Shortcut files (.lnk) support cmd.exe-style, unexpanded environment-variable references, which are expanded at the time the shortcut is opened, which, when logged in as the new user, will then expand to their profile dir.

Upvotes: 1

Related Questions