For two decades, I did not pay attention to creating or using PowerShell (PS) scripts/commands. I was not in the terminal, script mode. My time are in Visual Studio, Rider, or Visual Studio Code. Some short of IDE.
I have changed since I started Claude Code. More time in terminal. Started to enjoy the command line (CLI) world. I like both.
I like to clean my local branches. When a PR is completed, the remote branch is deleted. These two commands in PS Terminal will do the job
I have changed since I started Claude Code. More time in terminal. Started to enjoy the command line (CLI) world. I like both.
I like to clean my local branches. When a PR is completed, the remote branch is deleted. These two commands in PS Terminal will do the job
git remote prune origin git branch -vv | select-string -Pattern ": gone]" | % {$_.toString().Trim().Split(" ")[0]} | % { git branch -D $_}
Copy and Paste. And job done!
Let me ask Claude Code to help me create a command that I can run in any repository.
I usually have a need to clean local branches that are no longer tracked with remote. Usually, I have to run `git remote prune origin` and a custom script to delete them locally. I want a command that can run in PS at any repository.
Claude Code: (sure buddy)
First, open your profile: notepad $PROFILE Then add this function: function Invoke-GitPrune { git remote prune origin git branch -vv | Where-Object { $_ -match '\[.*: gone\]' } | ForEach-Object { $branch = $_.TrimStart() -split '\s+' | Select-Object -First 1 git branch -D $branch } } Set-Alias gprune Invoke-GitPrune Save and reload with . $PROFILE, then just run gprune in any repo.
Follow the instruction, I have my gprune PS command.
To execute the command ". $PROFILE", first run this one to grant permission
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
I did not understand what "." does. Claude Code becomes my mentor quickly as I asked it to explain.
The leading . is PowerShell's dot-source operator — it runs a script in the current shell session rather than a child process. Without it: - & $PROFILE or just running the script spawns a child process, loads the function there, then exits — your current session sees nothing. With it: - . $PROFILE runs the script in your current session, so any functions/aliases defined in it become immediately available. It's the equivalent of source ~/.bashrc in bash.
And now, I can use the same approach to create custom commands for my needs. It is possible to add as many commands as you want in the profile file.
The interesting part is that I was engaged in the process. I had questions and asked. Claude Code answered and I had more. Finally, I absorted what I received and wrote them down.