PowerShell - s50600822/Notes GitHub Wiki
profile - similar to bash profile or zshrc
code C:\Users\PC\Documents\WindowsPowerShell\profile.ps1
while ~
is intepreted as C:\Users\PC
, code ~\Documents\WindowsPowerShell\profile.ps1
doesn't work. It's kind of retarded.
Create Dir
New-Item -Path C:\Users\PC\Website -ItemType Directory
find where an exec is
(Get-Command SomeExec).Path
powershell profile script is at
$PROFILE.CurrentUserAllHosts
powershell function
function cdy {
Set-Location "Z:\dev\code\youtube-dl\"
}
function ydl {
param (
[string] $url
)
python3 -m youtube_dl $url
}
function flatten {
param (
[string] $Path
)
Get-ChildItem $Path -Directory | ForEach-Object {
$ChildCount = (Get-ChildItem $_.FullName | Measure-Object).Count
if ($ChildCount -eq 1) {
$Child = Get-ChildItem $_.FullName
Move-Item $Child.FullName $_.Parent.FullName
Remove-Item $_.FullName
}
}
}
Set Alias
below is a bad example because the argument will not work
Set-Alias -Name cdy -Value "cd Z:\dev\code\youtube-dl\"
Remove Alias
Remove-Item alias:cdy
DNS
$domain = "services.gradle.org"
$ip = Resolve-DnsName $domain | Where-Object {$_.IPAddress -ne $null} | Select-Object -First 1 -ExpandProperty IPAddress
Write-Host "Resolved $domain to IP address: $ip"
get all H264
function Get-H264Mp4Files {
# Get all MP4 files in current directory and subdirectories
$files = Get-ChildItem -Path . -Recurse -Include *.mp4
# Loop through each file and check if it has H264 codec
$h264Files = foreach ($file in $files) {
# Use ffprobe to detect codec of the video stream
$codec = & ffprobe.exe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 $file.FullName
# If codec is H264, output the file object
if ($codec -eq 'h264') {
$file
}
}
# Sort files by size
$h264Files | Sort-Object -Property Length
}
ToH265Mp4
function Convert-ToH265Mp4 {
param(
[Parameter(Mandatory=$true, Position=0)]
[ValidateScript({ Test-Path $_ -PathType Leaf })]
[string]$SourceFile
)
$SourceFileNoExt = [IO.Path]::GetFileNameWithoutExtension($SourceFile)
$OutputFile = "$SourceFileNoExt-h265.mp4"
$Cmd = "ffmpeg.exe -i `"$SourceFile`" -c:v libx265 -crf 28 -preset medium -c:a copy `"$OutputFile`""
& $Cmd
if (Test-Path $OutputFile) {
Write-Host "File converted successfully. Output file: $OutputFile"
}
else {
Write-Host "Failed to convert file."
}
}