I use cygwin version of gvim to edit files in windows, for that I created a bat script that opens a file with cygwin version of gvim(by converting the path to cygwin format). I also wrote a small powershell script to register this bat script with windows explorer so I can associate file extensions using the 'open with' context menu. Here is the script:
$ErrorActionPreference = "Stop"
$classes="hkcu:\software\classes"
$appid="cygwin.gvim"
$apps="$classes\applications"
$cmd='...SOMEDIRECTORY...\edit-gvim.bat'
$friendlyname='gVim (Cygwin)'
$icon='...ANOTHERDIRECTORY...\vim.ico'
$filetypes=#(".txt", ".ps1", ".sh", ".py", ".cs", ".cpp", ".c", ".rb",
".zsh", ".bash", ".vbox", ".xml", ".yml", ".yaml", ".bat")
if (test-path "$apps\$appid") {
# cleanup
remove-item -recurse "$apps\$appid"
}
# register open commands to know filetypes
new-item -path "$apps\$appid\shell\open\command" -value "$cmd `"%1`"" -force
# add a context menu item(edit with gVim) to every file in windows explorer
new-item -path "$classes\*\shell\Edit with $friendlyname\command" -value "$cmd `"%1`"" -force
# friendly name for the 'open with' dialog
new-itemproperty -path "$apps\$appid\shell\open" -name 'FriendlyAppName' -value $friendlyname
# register the icon
# FIXME this has no effect
new-item -path "$apps\$appid\DefaultIcon" -value $icon -type expandstring
# register supported file extensions
new-item -path "$apps\$appid\SupportedTypes"
foreach ($ext in $filetypes) {
new-itemproperty -path "$apps\$appid\SupportedTypes" -name $ext -PropertyType string -value ''
}
Everything works except the line below the 'FIXME' comment, which aparently has no effect. Instead of seeing my provided icon with applications associated with gvim, I see windows default icon for unknown file types. What am I missing here?
Here are some resources I used to write this script:
http://msdn.microsoft.com/en-us/library/windows/desktop/ee872121(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/cc144158(VS.85).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/cc144101(v=vs.85).aspx
Create registry entry to associate file extension with application in C++
I can make a mistake, but as far as I understand the syntax of DefaultIcon is :
"Full path to a file,ordinal"
This is usefull when you want to point to a resource in an EXE or a DLL
C:\Program Files (x86)\PowerGUI\ScriptEditor.exe,1
But you have to keep the same syntax if you want to point to an icon file :
[HKEY_CLASSES_ROOT\AcroExch.acrobatsecuritysettings.1\DefaultIcon]
#="C:\\Windows\\Installer\\{AC76BA86-7AD7-1036-7B44-A95000000001}\\PDFFile_8.ico,0"
So, in your case, I would try :
$icon='...ANOTHERDIRECTORY...\vim.ico,0'
or
$icon='...ANOTHERDIRECTORY...\\vim.ico,0'
Edited
Don't forget to restart explroer.exe to see the new icon.
Related
Is there a way of extending the answer to this question to include the string " New Folder". e.g. "20221029 New Folder"
Current workaround that I am using in this Registry Key
Computer\HKEY_CLASSES_ROOT\Directory\Background\shell\NewFolderWithDate\command
is
cmd.exe /c powershell New-Item -ItemType Directory -Path ".\$((Get-Date).ToString('yyyyMMdd New'))"
This works in cmd.exe but with no spaces:
powershell New-Item -ItemType Directory -Path ".\$((Get-Date).ToString('yyyyMMdd'))NewFolder"
Use the following (note that there is no need to call via cmd /c):
powershell "New-Item -ItemType Directory -Path \".\$((Get-Date).ToString('yyyyMMdd New'))\""
Use \" to escape " characters that you want PowerShell to retain as part of the PowerShell command to execute, after command-line parsing - see this answer for more information.
Embed the command to pass (to the implied -Command CLI parameter) as a whole in "..." so as to prevent whitespace normalization; that is, without it, the two spaces before New in your command would become one.
I am trying to delete a binary value within my registry with this code the code prompts an error stating the value at DefaultConnectionSettings does exist but it's able to find the SID path, but not the exact DefaultConnectionSettings Value. I'm running this script on a test machine that has the DefaultConnectionSettings.
RegEdit.exe screenshot
Any input would be helpful Thanks,
if (!(Test-Path 'HKU:\')) {
New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS
}
($path = Get-ChildItem -Path 'HKU:\*\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections' -ErrorAction SilentlyContinue) |
ForEach-Object { Remove-ItemProperty -Path $path -name "DefaultConnectionSettings" -force }
The registry value you're trying to delete is a value of the registry::HKEY_USERS\*\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections registry keys themselves.
Note that you can target the HKEY_USERS hive simply by prepending the provider prefix registry:: to the native registry path - no need to map a new drive with New-PSDrive first.
By contrast, Get-ChildItem looks for subkeys of the targeted keys.
Note that registry values are considered properties of registry keys, not child items (the way that files are in the file-system).
Thus, the immediate fix is to switch from Get-ChildItem to Get-Item, which returns objects representing the target keys themselves.
However, you can do it all with a single Remove-ItemProperty call (as with your own attempt, running from an elevated session is assumed):
$path = 'registry::HKEY_USERS\*\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections'
Remove-ItemProperty -WhatIf -Force -Path $path -Name DefaultConnectionSettings -ErrorAction SilentlyContinue
Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.
Hi I want to create several shortcuts at same time using powershel and something like this
Get-ChildItem -Path D:\something\ -Include *.exe -File -Recurse -ErrorAction SilentlyContinue
get the results and generate shortcuts(.lnk files) for all .exe files
(.exe is just one example of file type)
Can u help me? thx
To create shortcuts of all your .exe files in a directory, you can do the following:
Create Windows Script host COM object to create shortcuts. You can have a look at Creating COM Objects with New-Object from MSDN for more information.
Get all .exe files in a directory. Similar to what you have done already with Get-ChildItem.
Iterate each of these files. Can use foreach or Foreach-Object here.
Extract BaseName from files. This means getting test from test.exe. We need this to make the shortcut file.
Create shortcut from path. This path is just the destination path + filename + .lnk extension. We can use Join-Path here to make this path.
Set target path of shortcut to the executable and save shortcut.
Demonstration:
$sourcePath = "C:\path\to\shortcuts"
$destinationPath = "C:\path\to\destination"
# Create COM Object for creating shortcuts
$wshShell = New-Object -ComObject WScript.Shell
# Get all .exe files from source directory
$exeFiles = Get-ChildItem -Path $sourcePath -Filter *.exe -Recurse
# Go through each file
foreach ($file in $exeFiles)
{
# Get executable filename
$basename = $file.BaseName
# Create shortcut path to save to
$shortcutPath = Join-Path -Path $destinationPath -ChildPath ($basename + ".lnk")
# Create shortcut
$shortcut = $wshShell.CreateShortcut($shortcutPath)
# Set target path of shortcut to executable
$shortcut.TargetPath = $file.FullName
# Finally save the shortcut to the path
$shortcut.Save()
}
I'm trying to place the taskbar location to the top with powershell and let it auto-hide.
I found this website with option three what works for most people. The only problem is I'm trying to get this fixed in Windows 10 build 14393.
https://www.sevenforums.com/tutorials/1066-taskbar-move-location-desktop-screen.html
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\StuckRects2]
I came across one forum who told that it was changing a registry value. I tried to do so but it didn't work. When I changed the location to the top, I looked in the regedit again and the same value was still here.
Does anyone know the location of where the registry key is stored in?
$RegKey = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\StuckRects2"
$RegName = "Settings"
Set-ItemProperty -Path $RegKey -Name $RegName -Value $RegValue
Does anyone knows what I need to change?
A PS Script to toggle taskbar top or bottom, restarts Explorer in the process.
$RegistryPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\StuckRects3'
$Name = "Settings"
$NewValue = Get-ItemProperty -Path $RegistryPath
$NewValue.Settings[12] = 4-$NewValue.Settings[12]
Set-ItemProperty -Path $RegistryPath -Name $Name -Value $NewValue.Settings
Stop-Process -Name "Explorer"
I'd like to add a Windows right-click menu entry to quickly open text files in WSL Vim, without manually opening up a terminal.
Thanks #romainl.
Create a registry key at Computer\HKEY_CLASSES_ROOT\*\shell named Open in Vim, and a subkey for it as command:
Computer\HKEY_CLASSES_ROOT\*\shell\Open in Vim
Computer\HKEY_CLASSES_ROOT\*\shell\Open in Vim\command
Edit the string value of the command\(Default) as bash.exe -c "wslpath '%1' | xargs nvim" (alternatively, you can substitute nvim with vim).
Download a Vim logo which you can convert online to an .ico extension. Create an Icon string value under the Open in Vim key, and edit its value with the path of the .ico image.
You can see the resulting entry below. Clicking on it would open up a WSL terminal with the selected textfile opened in Vim. Upon exitting Vim, the terminal closes, too.
Add an item to the context menu to open a file in WSL VIM with PowerShell:
New-Item -Path 'Registry::HKEY_CLASSES_ROOT\*\shell\VIM\command' -Force | Out-Null;
Set-ItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\`*\shell\VIM' -Name '(default)' -Value 'Open in VIM' -Force | Out-Null;
Set-ItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\`*\shell\VIM' -Name 'Icon' -Value 'C:\Windows\System32\wsl.exe,0' -Force | Out-Null;
Set-ItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\`*\shell\VIM\command' -Name '(default)' -Value "wsl vim -p `"`$(wslpath '%1')`"" -Force | Out-Null;
Or use a registry file as an alternative.
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\VIM\command]
#="wsl sudo vim -p \"$(wslpath '%1')\""