Skip to content

Instantly share code, notes, and snippets.

@alsolovyev
Last active October 16, 2024 03:09
Show Gist options
  • Save alsolovyev/e3e912f728b464b8738dd5c599f6da24 to your computer and use it in GitHub Desktop.
Save alsolovyev/e3e912f728b464b8738dd5c599f6da24 to your computer and use it in GitHub Desktop.
Powershell scripts
<#
.SYNOPSIS
Edit Windows Context Menu
.DESCRIPTION
Add or remove options "Open with <program_name>" for certain file types in the Windows Context Menu
.PARAMETER Remove
Allows to delete previously created options in the Window Context Menu
.EXAMPLE
PS> ./add_programs.ps1
.EXAMPLE
PS> ./add_programs.ps1 -Remove $True
.LINK
https://github.com/alsolovyev
#>
# Check passed parameter
param([bool]$Remove=$False)
# Check administrator rights (#Requires -RunAsAdministrator)
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Warning 'Access denied. Run the script as administrator.' ; break
}
$exts = @('.js', '.html', '.sass', '.css', '.md', '.txt', '.bat', '.ps1', '.reg')
$programs = @(
@{
name = 'subl'
title = 'Open with Sublime Text'
command = '"C:\Program Files\Sublime Text 3\sublime_text.exe" "%1"'
icon = 'C:\Program Files\Sublime Text 3\sublime_text.exe,0'
},
@{
name = 'vscode'
title = 'Open with VS Code'
command = '"C:\Program Files\Microsoft VS Code\Code.exe" "%1"'
icon = 'C:\Program Files\Microsoft VS Code\Code.exe,0'
}
)
$path = 'HKLM:\SOFTWARE\Classes\SystemFileAssociations'
# Iterate over all extensions
foreach ($ext in $exts) {
if (-not $Remove) {
# Add keys
if (-not (Test-Path -Path $path\$ext)) {
New-Item -Path $path\$ext -Force | out-null
}
if (-not (Test-Path -Path $path\$ext\shell)) {
New-Item -Path $path\$ext\shell -Force | out-null
}
# Iterate over all programs
foreach ($program in $programs) {
if (-not (Test-Path -Path $path\$ext\shell\$($program.name))) {
New-Item -Path $path\$ext\shell\$($program.name) -Force | out-null
}
if (-not (Test-Path -Path $path\$ext\shell\$($program.name)\command)) {
New-Item -Path $path\$ext\shell\$($program.name)\command -Force | out-null
}
Set-ItemProperty -Path $path\$ext\shell\$($program.name) -Name '(Default)' -Value $program.title
Set-ItemProperty -Path $path\$ext\shell\$($program.name) -Name 'Icon' -Value $program.icon
Set-ItemProperty -Path $path\$ext\shell\$($program.name)\command -Name '(Default)' -Value $program.command
}
} else {
# Remove keys
if (Test-Path -Path $path\$ext) {
Remove-Item -Path $path\$ext -Recurse -Force
}
}
}
function mk {
<#
.SYNOPSIS
Create a directory and navigate into it
.DESCRIPTION
.
.EXAMPLE
mk directory_name
.LINK
https://github.com/alsolovyev
#>
param(
# Directory name.
# Aliases: N
[Parameter(Mandatory=$true, Position=0)]
[Alias("N")]
[String] $Name
)
if (-not (Test-Path -LiteralPath $Name)) {
# New-Item -Path $Name -ItemType Directory | Out-Null
# Set-Location -Path $Name
try {
New-Item -Path $Name -ItemType Directory -ErrorAction Stop | Out-Null #-Force
Set-Location $Name
}
catch {
Write-Error -Message "Unable to create directory '$Name'. Error: $_" -ErrorAction Stop
}
}
else {
# Write-Warning "The directory `"$Name`" already exists"
Write-Host "The directory `"$Name`" already exists." -ForegroundColor Red
}
}
$host.ui.RawUI.WindowTitle = "kali"
function touch {
<#
.SYNOPSIS
Create a file
.DESCRIPTION
.
.EXAMPLE
touch file_name
.LINK
https://github.com/alsolovyev
#>
param(
# File name.
# Aliases: N
[Parameter(Mandatory=$true, Position=0)]
[Alias("N")]
[String] $Name
)
# check file existence
if (-not (Test-Path -LiteralPath $Name)) {
# New-Item -Path $Name -ItemType File | Out-Null
# echo $null >> $Name
try {
New-Item -Path $Name -ItemType File | Out-Null #-Force
}
catch {
Write-Error -Message "Unable to create file '$Name'. Error: $_" -ErrorAction Stop
}
} else {
# Write-Warning "The file $Name already exists"
Write-Host "The file `"$Name`" already exists." -ForegroundColor Red
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment