Trying to reach out to one server (by name) and search for the Profile DIR for each username's existence from the list of users, whom I need to delete. After many many attempts, still seeing a result of "$User profile not found." Even though I can see that it is clearly in the DIR. When I run one specific test for one user, it shows up.. example: `
$DelUser = "user.name"
$ProfileUser = "\\dc1\P$\Profiles\$DelUser" ".V6"
if ([System.IO.Directory]::Exists($ProfileUser)) {
"......$DelUser..... EXISTS!"
else {
"$DelUser Path NOT FOUND!"
}
}
Pulling from the source (txt) file (I've also tried using a csv file with import-csv
with same results) I try to go through the list and search for the existence of each user folder with the below code.
User folders are in the user.name.V6
format.
Below a sample with some commented alternate attempts.
$DelUser = Get-Content -Path C:\DelFolder\Delete-DC1-Users.txt
$UserPath = "\\DC1\P$\Profiles\$User" ".V6"
foreach ($User in $DelUser) {
if ([System.IO.Directory]::Exists($UserPath)) {
Write-Host "$UserPath EXISTS"
#if (Test-Path -Path $UserPath) {
# if ($UserPath) {
# Write-Host "$User Folder Exists"
}
else {
Write-Host "$User Folder DOES NOT EXIST"
}
}
And at best I'll see the $User Folder DOES NOT EXIST
even though it does in fact exist on the server.
I've also tried using Test-Path
to no avail, as seen above in the commented out lines.
Thank you! P.S. My first posted question here, so my apologies for any mishaps or mistakes!
CodePudding user response:
$UserPath = "\\dc1\P$\Profiles\$User" ".V6"
Your use of "..."
, i.e. of an expandable (interpolating) string , means that the reference to variable $User
is instantly expanded, resulting in storing a static string in variable $UserPath
; that is, you're "baking in" the value of $DelUser
at that time (and since $User
isn't defined until later, the expansion will will result in the empty string by default).
What you need instead is to move your $UserPath = ...
assignment inside your foreach
loop so that the "..."
is string is re-evaluated in each iteration, based on the then-current value of $User
:
$delUser = Get-Content -LiteralPath C:\DelFolder\Delete-DC1-Users.txt
foreach ($user in $delUser) {
# Determine the path *for the $user at hand*.
$userPath = "\\DC1\P$\Profiles\$user.V6"
if (Test-Path -LiteralPath $userPath) {
"$userPath EXISTS"
}
else {
"$user Folder DOES NOT EXIST"
}
}
Note:
The
".V6"
suffix was folded directly into the"..."
string - no need for appending it separately with- Note that such a suffix is not treated as an attempt to access a property of the
$user
variable - for the latter, you'd need enclosure in$(...)
- see this answer for a summary of PowerShell's string-interpolation rules.
- Note that such a suffix is not treated as an attempt to access a property of the
It is safer to use
-LiteralPath
rather than-Path
when you know you're dealing with literal (verbatim) paths rather than wildcard expressions.You could add
-Type Container
to theTest-Path
call to limit the test to whether a directory (rather than a file) with the given path exists, though that's probably not necessary here.The
Write-Host
calls were omitted in favor of implicit output of the strings (impliedWrite-Output
), though note that the former prints directly to the display,[1] whereas the latter produces data output that can be captured.- See this answer for more information.
While not enforced, a worthwhile convention to observe is to start the names of regular variables with lowercase letters (e.g,
$user
) and to only start parameter variables with an uppercase letter (e.g.$Path
inside aparam(...)
block).
Note that there are ways to perform string templating, where string expansion (interpolation) is performed on demand on a technically verbatim string, as shown in this answer.
However, in the case at hand the solution above is simpler.
[1] In PSv5 Write-Host
writes to the information stream, whose output can be captured, but only via 6>
.