I'm trying to convert a command line script for an SCCM SMS program uninstallation process into PowerShell. The command that works in CMD is: `dir /b *.mof *.mfl | findstr /v /i uninstall > moflist.txt & for /F %%s in (moflist.txt) do mofcomp %%s`.
I've managed to create this PowerShell command:
```powershell
Pushd "C:WindowsSystem32wbem"
Get-ChildItem -Filter {Name -like "*.mof" -or Name -like "*.mfl"}).FullName | Where-Object {(Get-Content $_) -notcontains "uninstall"} | ForEach-Object {mofcomp $_}
```
But I can't get this to work:
```powershell
Get-ChildItem -Path "C:WindowsSystem32wbem" -Filter {Name -like "*.mof" -or Name -like "*.mfl"}).FullName | Where-Object {(Get-Content $_) -notcontains "uninstall"} | ForEach-Object {mofcomp $_}
```
I'm getting an error message saying `Get-Content : cannot find path x: file because it does not exist`. It seems like it's not looking in the specified path at all. I would like to avoid changing directories in my script, and while I haven't tested as an admin yet, I need to make sure it throws an 'access denied' error as a normal user during testing. Any insights on what might be going wrong here?
1 Answer
You might want to try this command instead:
```powershell
Get-ChildItem C:WindowsSystem32wbem* -Include *.mof, *.mfl | Select-String uninstall -NotMatch -List | ForEach-Object { mofcomp $_.Filename }
```
This version should work better. Also, for a shorter version, you could use aliases like this:
```powershell
gci C:WindowsSystem32wbem* -i *.mof, *.mfl | sls uninstall -NotMatch -List | % { mofcomp $_.Filename }
```
Here's another option you might consider:
```powershell
Select-String -Path C:WindowsSystem32wbem* -Include *.mof, *.mfl -Pattern uninstall -NotMatch -List |
ForEach-Object { mofcomp $_.Path }
```
Just keep in mind that these scripts, including what you have, aren't directly equivalent to your CMD code. The CMD command checks file names for the absence of "uninstall", while the PowerShell versions you provided check file content instead.