Search code examples
powershellobjectregistrycounting

Counting registry entries in powershell


I am currently working on a project where I am installing some chrome and edge extensions on to computers in our environment via the SOFTWARE\Policies\Microsoft\Edge\ExtensionInstallForcelist location in the registry.

I am trying to work out a way where I can script a count of the number of entries in that list and then do a +1 for the name of the next application if I need to install a new one to save hard coding the numbers into my script. I have tried looking around for ideas on a way to do it but I am not sure how to phrase my question to find the answer to my specific question. I am still really new to powershell and I am just writing my first few scripts and I am sure there is something silly that I am overlooking or just plain not thinking of right now. I have tried the following on my blocklist registry item because I know there is more than 1 in there.

PS C:\WINDOWS\system32> get-RegValue -Hive LocalMachine -key SOFTWARE\Policies\Microsoft\Edge\ExtensionInstallBlocklist |Select-Object -Property value

Value       
-----       
1           
New Value #1



PS C:\WINDOWS\system32> get-RegValue -Hive LocalMachine -key SOFTWARE\Policies\Microsoft\Edge\ExtensionInstallBlocklist |Select-Object -Property value | measure


Count    : 2
Average  : 
Sum      : 
Maximum  : 
Minimum  : 
Property : 




PS C:\WINDOWS\system32> get-RegValue -Hive LocalMachine -key SOFTWARE\Policies\Microsoft\Edge\ExtensionInstallBlocklist |Select-Object -Property value | measure |Select-Object -Property count

Count
-----
    2



PS C:\WINDOWS\system32> $Value = get-RegValue -Hive LocalMachine -key SOFTWARE\Policies\Microsoft\Edge\ExtensionInstallBlocklist |Select-Object -Property value | measure |Select-Object -Property count

PS C:\WINDOWS\system32> [int]$Value 
Cannot convert the "@{Count=2}" value of type "Selected.Microsoft.PowerShell.Commands.GenericMeasureInfo" to type "System.Int32".
At line:1 char:1
+ [int]$Value
+ ~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : ConvertToFinalInvalidCastException
    

The issue that I am running into is I am not sure how to return the count as an integer? or to at least turn the count from a string to an integer so that I can then do a +1 loop on it.

My end goal is to have it read the registry, count the number of strings in it, then +1 for the name for the next entry and then set the data for the entry. As I said I can easily hard code it so that app 1 is "1" and app 2 is "2", but I think it would be better if I could ask it to count the entries and the +1 for the name.


Solution

  • @{Count=2}" is indicating that this is an object with a property called Count. You are getting that error because you cannot cast the whole object to an integer.

    You can however access the value through it's property

    $Value.Count
    

    Or instead you can do this in your main statement using -ExpandProperty Count which would only return the value of the property instead of an object containing the property.

    get-RegValue -Hive LocalMachine -key SOFTWARE\Policies\Microsoft\Edge\ExtensionInstallBlocklist | 
        Select-Object -Property value | 
            Measure-Object | 
                Select-Object -ExpandProperty count