Saturday, June 18, 2022

PowerShell 004 - PowerShell Dot Notation

Powershell Dot Notation

Dot notation gives you the ability to access properties nested within an object, this can be used to pull or modify data. I will be using Chrome preferences as my example file, see below.


        $username = (Get-WMIObject -ClassName Win32_ComputerSystem).Username.Split('\')[1]

This will set the variable $username as the currently logged in user without its domain included.


        $pref = Get-Content (“C:\Users\” + $username + ”\AppData\Local\Google\Chrome\User Data\Default\Preferences”) | ConvertFrom-JSON

This will set $pref as the contents of the user’s Chrome preferences and convert it into a more readable format for powershell.




You can use dot notation in conjunction with math operations to lead to expectable results. Anything between (parenthesis) will be done first, you can use a plus (+) as a way to add text together. In the examples above I set the variable “$username” to dynamically update based on who is currently logged in. I used this to add it in between two sets of exact text indicated by quotes (“”), and finally contained it all with parenthesis () to indicate this should be resolved before Get-Content. The piped in ConvertFrom-JSON will format this in a readable format for powershell.