Skip to content

Instantly share code, notes, and snippets.

@SARDONYX-sard
Last active September 14, 2023 21:44
Show Gist options
  • Save SARDONYX-sard/9151053335bf5eeb588f0225226011fe to your computer and use it in GitHub Desktop.
Save SARDONYX-sard/9151053335bf5eeb588f0225226011fe to your computer and use it in GitHub Desktop.
My Powershell scripts(Backup savedata, delete Program files, bluetooth)
# Usage:
# .\delete.ps1
# Check if the script is running with administrative privileges
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
# Relaunch the script as an administrator
if (Get-Command pwsh) {
Start-Process pwsh -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File ./delete.ps1" -Verb RunAs
}else{
Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File ./delete.ps1" -Verb RunAs
}
exit
}
function Delete-Directories {
param (
[string[]]$directories
)
foreach ($directory in $directories) {
# Check if the directory exists
if (Test-Path -Path $directory) {
# Take ownership of the directory and its subdirectories
takeown.exe /f $directory /r
# Grant full control to administrators on the directory and its subdirectories
icacls.exe $directory /grant administrators:F /t
# Remove the directory and its contents
Remove-Item -Path $directory -Force -Recurse
} else {
Write-Host "The '$directory' directory does not exist."
}
}
}
Delete-Directories -directories @(".\windowsapps", ".\Program Files")
# ref:
# - https://www.bluetoothgoodies.com/battery-monitor/faq/
# - https://stackoverflow.com/questions/71736070/how-to-get-bluetooth-device-battery-percentage-using-powershell-on-windows
Param([switch]$d, [switch]$isDebug)
$isDebug = $d.IsPresent -or $isDebug.IsPresent
$local:BatteryLevel = '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2'
$local:IsConnected = "{83DA6326-97A6-4088-9453-A1923F573B29} 15"
$local:LastConnectedTime = "{2BD67D8B-8BEB-48D5-87E0-6CDA3428040A} 11";
$local:LastConnectedTime2 = "{2BD67D8B-8BEB-48D5-87E0-6CDA3428040A} 5";
$local:Result = @();
Get-PnpDevice -Class "System" | ForEach-Object {
# $_ | Format-List -Property *
$local:test = $_ | Get-PnpDeviceProperty -KeyName $BatteryLevel | Where-Object Type -NE Empty;
if ($test)
{
$local:Properties = @{ "friendlyName" = $_.friendlyName };
Get-PnpDeviceProperty -InstanceId $($test.InstanceId) `
-KeyName $BatteryLevel, $IsConnected, $LastConnectedTime, $LastConnectedTime2
| ForEach-Object {
$Properties += @{ $_.KeyName = $_.Data }
}
$Result += $Properties
}
}
$Result | ConvertTo-Json
Read-Host "Press any key."
from os import system
from shutil import copy2, copytree
def remove(path: str):
"""To avoid permission error"""
system(f"powershell.exe Remove-Item -Recurse -Force \"{path}\"")
base_dir = "C:/Users/SARDONYX/AppData"
plugin_path = "plugin/packer_compiled.lua"
nvim_file = f"{base_dir}/Local/nvim/{plugin_path}"
lvim_file = f"{base_dir}/Local/lvim/{plugin_path}"
packer_dir = "site/pack/packer"
nvim_packer_dir = f"{base_dir}/Local/nvim-data/{packer_dir}"
lvim_packer_dir = f"{base_dir}/Roaming/lunarvim/{packer_dir}"
remove(nvim_file)
copy2(lvim_file, nvim_file)
remove(nvim_packer_dir)
print("Copying packer lvim to nvim.")
print("Please wait...")
copytree(lvim_packer_dir, nvim_packer_dir)
# モンスターハンターワールドのセーブデータをバックアップするためのPowerShellスクリプト
# バックアップ元のファイルパス
$file = "C:/Program Files (x86)/Steam/userdata/406621538/582010/remote/SAVEDATA1000"
# バックアップ先のディレクトリ
$backupDir = Get-Location
# バックアップファイルの拡張子
$extension = ".bak"
# バックアップファイル名のベース
$baseName = "SAVEDATA1000-"
# バックアップファイルの最大数
$maxBackupCount = 5
# バックアップファイルのリストを取得
$backupFiles = Get-ChildItem $backupDir | Where-Object { $_.Name -match "^$baseName\d+$extension$" } | Sort-Object -Descending
# 最新のバックアップファイルの番号を取得
if ($backupFiles.Count -gt 0) {
$latestBackupNumber = [int]($backupFiles[0].Name -replace "^$baseName|\D|\s|\t|$extension")
} else {
$latestBackupNumber = 0
}
# 新しいバックアップファイル名を生成
$newBackupNumber = ($latestBackupNumber % $maxBackupCount) + $maxBackupCount # バックアップ上限に達したら最新ファイルを上書き保存
$newBackupFile = "$backupDir\$baseName$newBackupNumber$extension"
# バックアップファイルをコピー
Copy-Item $file $newBackupFile
# バックアップ完了メッセージを表示
Write-Host "Backup file created: $newBackupFile"
$secondsRunning = 0;
Write-Output "何かボタンを押してください。(この画面は自動で5秒後に消えます)"
while( (-not $Host.UI.RawUI.KeyAvailable) -and ($secondsRunning -lt 5) ){
Write-Host ("Waiting for: " + (5-$secondsRunning))
Start-Sleep -Seconds 1
$secondsRunning++
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment