PowerShell and Shift + Ins, or how to gain Hermes speed when working with GPP

    The clipboard and PowerShell will speed up, but not blind.

    Using GPP to add registry files is creepy and inconvenient - all these registry branches, key type, values ​​... Especially if branches and values ​​are pretty. But there are a couple lifehacks that can significantly speed up work with group policies.

    You can, of course, hang a logon script with the command to import the registry branch. But this is not our method.

    Method one. Slightly complicated

    The first option is to create your own GPO template. This method is especially useful if you need to change the parameters of values ​​depending on the user. If you do not want to learn the principles of formation of templates, you can simply export the "right» reg-file and convert it to a template file using a script REG_2_ADMXL.vbs , issued in the gallery Technet script.

    Suppose we want to make life easier for users by screwing several search engines to Internet Explorer as follows:

    • If you type “ g query text” in the address bar , the query would be searched on Google;
    • Y request text” - in Yandex;
    • and “ w request text” is in Russian Wikipedia.

    To do this, just prepare the registry file:

    Windows Registry Editor Version 5.00
    [HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\SearchUrl\W]
    [HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\SearchUrl\Y]
    [HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\SearchUrl\G]

    Convert it to a template with the following command:

    cscript REG_2_ADMXL.vbs IE-search.reg Ru-ru IE-search.admx

    The resulting template and folder with the language file we drop into the directory with the templates. Now our policy will appear in the GPO management snap-in.

    Installed template.

    The mechanism is not very convenient in terms of maintenance, but in principle it works. And do not manually drive anything.

    Another option would be to use PowerShell cmdlets to work with GPOs. For example, to restore the context menu item “Send”, the set of cmdlets will be as follows:

    Import-module -Name GroupPolicy
    New-GPO -Name SendTo
    Set-GPRegistryValue -Name "SendTo" -key "Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" -ValueName SendTo -Type ExpandString -value "C:\Users\Default\AppData\Roaming\Microsoft\Windows\SendTo"
    Set-GPLink -Name SendTo -Target "ou=Users,dc=domain,dc=com" -LinkEnabled Yes

    This is the option for just one key. If there are a lot of keys, then for convenience you will have to reinvent the wheel and again convert the registry file into a set of PowerShell cmdlets.

    You can read more about working with group policies and automating all of this in one of the previous articles, " Immersion in Templates and Taming Windows GPO. " Well, I'll tell you about my favorite way.

    The second way. Copy - Paste (almost)

    As you may have noticed (I didn’t notice that very immediately), in the Group Policy Management snap-in, actions are available on objects, including the standard clipboard operations.

    Context menu in GPP registry settings.

    If you copy an object to the clipboard and paste it, it turns out that this object is a file in xml format. This means that the file can be pre-formed and added to the snap-in without filling the fields manually.

    For this, Malcolm McCaffery wrote a special script. It generates an xml file based on the exported registry file. The script can be taken in the blog of the author.

    In the script, the author has extra calls to the function Convert-Reg2Xml and an extra block of parameters. These errors are easily corrected during a test run via PowerShell ISE. Nevertheless, I just in case threw the corrected version on pastebin . There is also a fork of this script on github , and even an online service that does the same.

    Using the script is extremely simple.

    Convert-Reg2Xml -regPath input.reg -xmlPath output.xml

    The resulting xml can even be dragged into the box for registry settings.

    Let us consider a specific example. To begin with, we export the "correct" registry branch. In this example, we will make a policy to enable the display of hidden files, folders, and extensions, and at the same time we will make ctfmon.exe autorun for a smooth change of layout.

    The registry file is:

    Windows Registry Editor Version 5.00
    "Language Bar"=""ctfmon”=”CTFMON.EXE”

    Convert it to xml. If you look at the resulting file, it will already be like this:

    Full XML listing under spoiler.
    <?xml version="1.0" encoding="utf-8"?><Collectionclsid="{53B533F5-224C-47e3-B01B-CA3B3F3FF4BF}"name="HKEY_CURRENT_USER"><Collectionclsid="{53B533F5-224C-47e3-B01B-CA3B3F3FF4BF}"name="Software"><Collectionclsid="{53B533F5-224C-47e3-B01B-CA3B3F3FF4BF}"name="Microsoft"><Collectionclsid="{53B533F5-224C-47e3-B01B-CA3B3F3FF4BF}"name="Windows"><Collectionclsid="{53B533F5-224C-47e3-B01B-CA3B3F3FF4BF}"name="CurrentVersion"><Collectionclsid="{53B533F5-224C-47e3-B01B-CA3B3F3FF4BF}"name="Explorer"><Collectionclsid="{53B533F5-224C-47e3-B01B-CA3B3F3FF4BF}"name="Advanced"><Registryclsid="{9CD4B2F4-923D-47f5-A062-E897DD1DAD50}"name="Hidden"descr="Imported Reg File"image="17"><Propertiesaction="U"hive="HKEY_CURRENT_USER"key="Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"name="Hidden"default="0"type="REG_DWORD"displayDecimal="0"value="00000001" /></Registry><Registryclsid="{9CD4B2F4-923D-47f5-A062-E897DD1DAD50}"name="HideFileExt"descr="Imported Reg File"image="17"><Propertiesaction="U"hive="HKEY_CURRENT_USER"key="Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"name="HideFileExt"default="0"type="REG_DWORD"displayDecimal="0"value="00000000" /></Registry><Registryclsid="{9CD4B2F4-923D-47f5-A062-E897DD1DAD50}"name="ShowSuperHidden"descr="Imported Reg File"image="17"><Propertiesaction="U"hive="HKEY_CURRENT_USER"key="Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"name="ShowSuperHidden"default="0"type="REG_DWORD"displayDecimal="0"value="00000001" /></Registry></Collection></Collection><Collectionclsid="{53B533F5-224C-47e3-B01B-CA3B3F3FF4BF}"name="Run"><Registryclsid="{9CD4B2F4-923D-47f5-A062-E897DD1DAD50}"name="Language Bar"descr="Imported Reg File"image="7"><Propertiesaction="U"hive="HKEY_CURRENT_USER"key="Software\Microsoft\Windows\CurrentVersion\Run"name="Language Bar"default="0"type="REG_SZ"displayDecimal="0"value=""ctfmon”=”CTFMON.EXE”" /></Registry></Collection></Collection></Collection></Collection></Collection></Collection>

    Now you can simply paste it into the registry settings. Such beauty will turn out:

    Customized policy.

    With proper skill, the process takes less time than reading this text.

    For added convenience, you can add an item for registry files in the context menu to convert files in general in two clicks.

    It will be enough to create a new type of action for objects of type regfile and set the path to the desired script. For your convenience, I put it, of course, in the registry file:

    Windows Registry Editor Version 5.00
    @="powershell.exe -File C:\\temp\\script.ps1 %1"

    The script path is set in the last line.

    In order for the script to do this, you need to add the following lines to it:

    $output=(Split-Path -Path $args[0]) + "\output.xml"
    Convert-Reg2Xml -regPath $args[0] -xmlPath $output

    Now when you call the context menu, another item will appear, when you click on it, the xml file we need appears next to the registry file.

    New context menu item.

    Of course, such mechanisms are superfluous for making simple changes to the registry of users and computers. But for setting a large number of parameters, these life hacks are very convenient.

    Also popular now: