Helpful Functions For Your PowerShell Profile

mr.smashy
3 min readFeb 26, 2021

--

I work with PowerShell a lot so I have a long $Profile

I use Microsoft Terminal in Windows because it’s pretty dope.

What is a PowerShell Profile?

A PowerShell profile is a script that runs when PowerShell starts. It’s essentially like ~/.bashrc for the PowerShell environment. You can read all about the PowerShell profile here in the official Microsoft documentation.

Do You Have a Profile?

It’s a valid question, so to determine if you do, open a PowerShell session and enter:

$PROFILE | Get-Member -Type NoteProperty

If the response has values for all four values, especially CurrentUserCurrentHost, you have a profile. If not you can create one by using the following command:

if (!(Test-Path -Path $PROFILE)) {
New-Item -ItemType File -Path $PROFILE -Force
}

What Do You Put in Your Profile?

Now that you have a profile, which will run every time you launch PowerShell, you need to put some scripts in it that you’d like to run. I have two basic categories of scripts: helpful scripts and Linux-like commands. A lot of Linux commands are aliased in PowerShell; it even runs on Linux and I use it on Linux. But you can cat a file and it does what you would expect. But I put other scripts in my profile to add more Linux-like commands.

Helper Scripts

All these functions become new commands. If you define a function in your profile, you can type the function as a command in your session.

Get your current public IP

Function Get-PubIP {
(Invoke-WebRequest http://ifconfig.me/ip ).Content
}

Get the date and time in UTC

Function Get-Zulu {
Get-Date -Format u
}

Generate a pseudo random password

Function Get-Pass {
-join(48..57+65..90+97..122|ForEach-Object{[char]$_}|Get-Random -C 20)
}

Display system uptime

function uptime {
Get-WmiObject win32_operatingsystem | select csname, @{LABEL='LastBootUpTime';
EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
}

Reload profile

function reload-profile {
& $profile
}

Find a file

function find-file($name) {
ls -recurse -filter "*${name}*" -ErrorAction SilentlyContinue | foreach {
$place_path = $_.directory
echo "${place_path}\${_}"
}
}

Unzip a file

function unzip ($file) {
$dirname = (Get-Item $file).Basename
echo("Extracting", $file, "to", $dirname)
New-Item -Force -ItemType directory -Path $dirname
expand-archive $file -OutputPath $dirname -ShowProgress
}

Linux-like Scripts

grep

function grep($regex, $dir) {
if ( $dir ) {
ls $dir | select-string $regex
return
}
$input | select-string $regex
}

touch

function touch($file) {
"" | Out-File $file -Encoding ASCII
}

df

function df {
get-volume
}

sed

function sed($file, $find, $replace){
(Get-Content $file).replace("$find", $replace) | Set-Content $file
}

which

function which($name) {
Get-Command $name | Select-Object -ExpandProperty Definition
}

export

function export($name, $value) {
set-item -force -path "env:$name" -value $value;
}

pkill

function pkill($name) {
ps $name -ErrorAction SilentlyContinue | kill
}

pgrep

function pgrep($name) {
ps $name
}

Good Luck With Your Profile

I hope you found some useful functions to add to your profile. I use touch and grep quite a bit, and uptime is useful as I never shutdown my computers. PowerShell has gotten better over the years, and for some of us its something we have to live with.

--

--

mr.smashy
mr.smashy

Written by mr.smashy

Cybersecurity architect. Security dev and researcher. Infosec nerd. Linux enthusiast. All opinions and views are my own. Polite, professional, prepared.

Responses (1)