We want to eliminate the existence of a bad config file we've identified (and also have the manual fix for) from within every users appdata folder. The config file only exists here and there and not within every user. So I am trying to piece together a script that will return true/false if specified text is matched with get-content on config.xml. I want to run this against each user profile on every machine and output results to csv.
So far I've run into an issue. Still learning powershell so any help much appreciated.
$Users = "C:\Users"
$x = "This is the text I'm looking for"
Foreach ($Username in $Users)
{
$Path = "$Users\$Username\AppData\Local\MyApp\Settings\Config.xml"
$y = Get-Content -Path $Path
If ($y -match $x)
{
"true"
}
else
{
"false"
}
}
As you can see i haven't gotten round to the csv part yet because I'm not able to get the correct path to generate for get-content.
Rather than having the correct path which I hoped as the foreach loop worked it's way through would be:
"C:\Users\Username1\AppData\Local\MyApp\Settings\Config.xml"
"C:\Users\Username2\AppData\Local\MyApp\Settings\Config.xml"
"C:\Users\Username3\AppData\Local\MyApp\Settings\Config.xml"
I am instead getting the following error:
Get-Content : Cannot find path 'C:\Users\C:\Users\AppData\Local\MyApp\Settings\Config.xml' because it does not exist.
At C:\Users\mastaxx\Desktop\test.ps1:9 char:6
+ $y = Get-Content -Path $Path
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\C:\Use...ings\Config.xml:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
My seemingly silly assumption here is that $Users would be "C:\Users"
(which i believe it is because I've defined it) and $Username would be "Username1"
(which clearly it is not). Instead I can see it's also "C:\Users"
in this case, making the path being constructed to be this incorrect one:
C:\Users\C:\Users\AppData\Local\MyApp\Settings\Config.xml
I expected the foreach loop to treat each folder inside $Users
as an individual object and become the $Username
in each loop instance, allowing me to insert it into the config file $path
like so:
$Users $Username
↓ ↓
C:\Users\Username1\AppData\Local\MyApp\Settings\Config.xml
I'm not entirely sure what i'm doing wrong but i know I'm definitely doing something silly here. Would very much appreciate anyone with better PS skills to help point it out?