I am using PowerShell ISE (I think 4).
I am writing logon scripts to replace the old '*.BAT' files.
I am trying to test for a user-profile condition before 'creating/deleting' certain directories from the desktop.
If(($env:userprofile = "rmullins"))
{
Remove-Item $env:userprofile\Desktop\ITFILES -Recurse -Force
}
So I run the following to see what's going on:
md -Path $env:userprofile\Desktop\ITFILES
The path is created in the following location: C:\Windows\System32.........
The MD command above works fine until I run that 'IF' statement. I think I might not understand how the $env:userprofile
part works.
Any ideas?
The directory pointed at with the environment variable %userprofile% stores personal data of a specific user. The value of %userprofile% usually is c:\Users\username ( username being lowercase of %username% ?)
Environment variables in PowerShell are stored as PS drive (Env: ). To retrieve all the environment variables stored in the OS you can use the below command. You can also use dir env: command to retrieve all environment variables and values.
On Windows 7:
[PS]> echo $ENV:UserProfile
C:\Users\arco444
This returns the path to the profile directory. Therefore I'd expect looking only for the username to fail the condition. I'd do a simple match instead:
if ($env:userprofile -imatch "rmullins")
{
Remove-Item $env:userprofile\Desktop\ITFILES -Recurse -Force
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With