I found it:
[Environment]::UserName
$Env:UserName
There is also:
$Env:UserDomain
$Env:ComputerName
Answer from Thomas Bratt on Stack OverflowVideos
Part of my script involves making yourself a site admin for OneDrive data (set-spouser), then adding the manager, then removing yourself. Since I've always been the only one using this script, I've hardcoded my username into the script. However, other people will be using this script.
When it comes to connecting to exchange online or other admin portals, it's easy to do. You just leave the -userprincipalname field empty, and you get a popup box asking to enter your username, then your password. How do you "leave empty" which account you want to make a site admin for OneDrive data?
So I've been writing a script in powershell, and I'd like to make a few alterations to allow people to use it with minimal effort on their part. Part of this includes adjusting the script so that instances of my username in paths are replaced with $env:USERNAME.
For example,
C:\Users\{username}\Downloads\executable.exewould become
C:\Users\$env:USERNAME\Downloads\executable.exe
and the outcome would be the same.
I've used variables in paths in bash before with 0 issue, so I'm unsure of where I'm going wrong.
I've tried assigning it to a variable and calling it, and assigning $env:USERNAME and including that variable in the path but neither worked.
I've tried assigning to a variable as a string, and using Invoke-command $command but no dice.
How would I execute a script or an executable with a variable in the path name? Is this possible in powershell?
EDIT I asked in more detail on stackexchange, link below https://stackoverflow.com/questions/72615928/using-envusername-or-other-variables-when-executing-a-script Solved by assigning a variable that joined $env:USERPROFILE and the rest of the path.
$dom = $env:userdomain
$usr = $env:username
([adsi]"WinNT://$dom/$usr,user").fullname
Returns:
John Doe
Some other (mostly) obscure properties also available. A few useful ones:
- Homedrive UNC
- Homedrive Letter
- Description
- Login script
Try:
[adsi]"WinNT://
usr,user" | select *
I like the accepted answer, but just because I wanted to try this out myself:
$user = whoami
Get-WMIObject Win32_UserAccount | where caption -eq $user | select FullName
returns:
FullName
--------
TheCleaner
or if you wish to not have the header info and just the result:
$user = whoami
Get-WMIObject Win32_UserAccount | where caption -eq $user | select FullName | ft -hide