$i=0;$pnp = pnputil -e;
$matched = [regex]::matches($pnp, ".......................................Lexmark International");
$split = $matched -split (".........inf");
$replace = $split -replace " Driver package provider : Lexmark International","";
$replace1 = $replace -replace " ","`n";write-output $replace1;
foreach ($i in $replace1){;$pnpdel = pnputil -f -d $i;$pnpdel;};
Reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows x64\Drivers\Version-3\Lexmark Universal v2 XL" /f;
net stop spooler;net start spooler;start \\officechicprt5\111W-22E-CPRN-01
As you can hopefully see, my script tries to pull oem*.inf values from a pnpitil -e command. I am trying to get each oem##.inf file to be it's own variable in a For loop. My script is a bit of a mess with all the replaces and splits, but that part seems to get the part of the command that I need. I am having issues with the data in $i. It appears that the script will sometimes work, and sometimes not. I want pnputil -d oem99.inf for each oem# it finds in the pnputil enumeration. What am I doing wrong in my For loop? There has to be a better way... I'm still pretty new to this as you can tell.
Thanks again. Brent
Leveraging the power in PowerShell we can turn the output of
pnputil
into an object array that will make it much easier to parse the data you are looking for (since it appears you are looking for something specific).Each entry is a group of variables with a blank line in-between them. Using that lets turn this data into custom objects.
$rawdata
initially contains the text frompnputil -e
. We useSelect-Object -Skip 1
to remove the "Microsoft PnP Utility" line. Since$rawdata
is an array this approach requires that is it one long string so-join "`r`n"
. Immediately after we split it up into separate array elements of each property group with-split "`r`n`r`n"
which splits on the blank line you see in cmd output.The magic comes from
ConvertFrom-StringData
which create a hashtable from key value pairs in stings. In needs=
to work so we convert the colons as such. With each hashtable that is created we convert it to an object and save it to the variable$entries
.$entries
will always be an array since it is safe to expect more than one entry.Sample Entry once converted:
Now we can use PowerShell to filter out exactly what you are looking for!
Note that this can also return an array but for me there was only one entry. The output for this was
oem27.inf
Then using the information you are actually looking for you can run your other commands.