Press "Enter" to skip to content

Tag: windows

Windows: What ports does application/program X have open?

I recently had to help somebody with a Windows 10 based application which offered a “connection” facility (i.e. enter your computer’s IP address and port and the 3rd party system would connect to it), but the application failed to say which port(s) it had open.

Whilst there are a number of ways to view open ports – such as Microsoft’s Sysinternal’s TCPView and NirSoft’s CurrPorts – I wanted to suggest a way which neither required an additional piece of software to be installed and didn’t require administrator access.

Here are the steps to find out what ports application “AppName” has open:

Techy: Microsoft PowerShell and Linux Commands

I’m finding myself mix and matching between GNU/Linux and Microsoft PowerShell quite a bit at the moment and which most Linuxy/Linuxesque (?) commands work fine under PowerShell, some are a little bit different.

Here’s a few which may be handy to know (the only order is that which I entered them). I’ve also added a couple of Windows commands where they are slightly easier to remember/shorter than PowerShell – and, me being me, included links to the appropriate documentation where possible.

Linux CommandPowerShell CommandDescription
which <commandname>
Example:
which composer
GNU Logo GNU “which”
Get-Command <commandname>
Example:
Get-Command composer
Powershell Logo PowerShell “Getting Environment Variables”
Find the location of an executable called <commandname>
In PowerShell, Get-Command can be referred to as just “gcm” so the example would become “gcm composer
printenv
GNU Logo GNU “printenv”
Get-Item -Path Env:
Powershell Logo PowerShell “Getting Environment Variables”
List all environment variables.
PowerShell also supports using “ls” to list environment variables – such as “ls env:
<variablename>="<value>"
Example:
HELLO="you guys"
Set-Item -Path Env:<variablename> "<value>"
Example:
Set-Item -Path Env:HELLO "you guys"
Powershell Logo PowerShell: “Change the value of an environment variable”
Sets an environment variable called <variablename> to the text <value>

PowerShell also supports using “$env:<variablename>="<value>” ” – such as ” $env:HELLO="you guys"
echo $<variablename>
Example:
echo $PATH
GNU Logo GNU “echo”
Get-ChildItem -Path Env:<variablename>
Example:
Get-ChildItem -Path Env:PATH
Powershell Logo PowerShell “Get A Selected Environment Variable”
Show the value of an environment variable called <variablename>
PowerShell also supports using “ls” to list environment variables – such as “ls env:PATH

echo <text>
Example:
echo "Hello there!"

GNU Logo GNU “echo”
Write-Output <text>
Example:
Write-Output "Hello there!"
Powershell Logo PowerShell “Write-Output”
Displays/prints a <text> string to the terminal
grep "<string>" <file>
Example:
grep "sort code" document.txt
GNU Logo GNU “grep”
Select-String "<string>" <file>
Example:
Select-String “sort code” document.txt
Powershell Logo PowerShell “Select-String”
Search a file <file> for the given text <string> using a regular expression

grep "<string>" <file>
Example:
grep "sort code" document.txt
GNU Logo GNU “grep”
findstr /R "<string>" <file>
Example:
findstr /R "sort code" document.txt
Windows LogoWindows Command: “findstr”
Search a file <file> for the given text <string> using a regular expression

ls | grep "<string>"
Example:
ls | grep "\.html"
GNU Logo GNU “ls”
ls | Out-String -Stream | Select-String "<string>"
Example:
ls | Out-String -Stream | Select-String “\.html”
Powershell Logo PowerShell “Select- String: Convert Pipline Objects”
Search a directory listing for a filename containing <string> using a regular expression
ls | grep "<string>"
Example:
ls | grep "\.html"
GNU Logo GNU “ls”
dir | findstr /R "<string>"
Example:
dir | findstr /R "\.html"
Windows LogoWindows Command: “findstr”
Search a directory listing for a filename containing <string> using a regular expression.
wget <url> --output-document <filename>
Example:
wget https://h.tld/f.gif --output-document o.gif
GNU Logo GNU “wget”
Invoke-WebRequest <url> -OutFile <filename>
Example:
Invoke-WebRequest https://h.tld/f.gif -OutFile o.gif
Powershell Logo PowerShell “Invoke-WebRequest”
Save a file at a URL <url> as a local file called <filename>
\
(backslash)
Example:
a \
b
GNU Logo GNU “The Backslash Character”

(backtick)
Example:
a
`
b
Powershell Logo PowerShell “…Line Continuation in Code…”
Allows a command to be split across multiple lines using the multiline separator/line continuation character.
&&
Example:
echo "A" && echo "B"
GNU Logo GNU “Lists
&&
Example:
Write-Output "A "&& Write-Output "B"
Powershell Logo PowerShell “Pipeline Chain Operators”
Command chaining using pipeline operators/list operators – if the condition on the left is true/passes, then continue.
rm <file>
Example:
rm test.tmp
GNU Logo GNU “rm”
Remove-Item <file>
Example:
Remove-Item test.tmp
Powershell Logo PowerShell “Remove-Item”
Deletes a file called <file>
unzip <file>
Example:
unzip myfile.zip
Expand-Archive <file>
Example:
Expand-Archive myfile.zip
Powershell Logo PowerShell “Expand-Archive”
Extracts a .zip archive file
uname
Example:
uname -nrmo
GNU Logo GNU “uname”
(note, the output order of uname in GNU/Linux cannot be altered)
Get-ComputerInfo
Example:
Get-ComputerInfo -Property CsDNSHostName, OsVersion, OsArchitecture, OsName | ConvertTo-Json
Powershell Logo PowerShell “Get- ComputerInfo”
Powershell Logo PowerShell “ConvertTo-Json”
Gets basic information about the system. For example:
Computer’s “hostname”
[GNU:n PS: CsDNSHostName]
OS or kernel release
[GNU: r PS:OsVersion]
Machine/Processor type
[GNU:m PS:OsArchitecture]
Operating system (OS) name
[GNU:o PS: OsName]
These commands were testing on a Debian bullseye Linux virtual machine and using Microsoft PowerShell 7.2 on Windows 10.

Microsoft PowerShell – don’t you mean Windows PowerShell?

Nope.

If you have a Windows 11, 10, 8.1, 8.0 or 7 machine, it would have come “with default” with Windows PowerShell – however, Microsoft has replaced that with a new “multiplatform” version which – in their infinite wisdom – have decided to call Microsoft PowerShell but haven’t “forcibly upgraded” people (you can upgrade yourself via their “Installing PowerShell on Windows” guide).

Microsoft do have a webpage about the differences between the PowerShells if you want to read: but if you use the command line a lot, it might be worth looking at Windows Terminal as well.

Which version of PowerShell do I have?

If you type/copy:

(Get-Host).Version

into the shell, you’ll get:

  • A “Major” version number of 7 or above = (new) Microsoft PowerShell
  • A “Major” version number of 5 or lower = (old) Windows PowerShell
  • A “Major” version number of 6 = I don’t think this actually exists and therefore should be used as the “cut-off” point between the two.
  • “.Version was unexpected at this time” = Probably using Windows Command Prompt
  • “-ash: syntax error: unexpected word” = You are in a shell of a Linux machine!
  • “bash: syntax error unexpected token `.Version’ = Linux again!

Real Life Example

As an example, here’s a single line Linux “command script” which uses curl to fetch a page from https://api.example/latest , parse it for the first occurrence of the text:

"tag_name":"v..."

(where v…. is any sequence of characters starting with “v” and ending with a quote mark) and store that as $VERSION then pass that to curl again to download that specific version zip file from https://example.com/v${VERSION}.zip as “example.zip” (replacing ${VERSION} with the extracted version number), unzip it and then delete the downloaded .zip file.

And, yes, I’ve colour coded the appropriate sections so you can see which command “maps” to which other command.

Linux Example

VERSION=$(
 curl --silent "https://api.example.com/latest" | \
 grep '"tag_name":' | \
 sed -E 's/.*"v([^"]+)".*/\1/' \
) && curl -L https://example.com/v${VERSION}.zip -o example.zip \
&& unzip example.zip && rm example.zip

Windows PowerShell 7 Example

if (((Invoke-WebRequest "https://api.example.com/latest").Content) -match '"tag_name":"v(?<ver>[^"]+)"') {
$VERSION=$Matches.ver &&
Invoke-WebRequest "https://example.com/v${VERSION}.zip" -OutFile example.zip &&
Expand-Archive example.zip && Remove-Item example.zip }

Getting Mailvelope working on Brave Browser

For the last few years, I’ve been using Brave as my primary web browser due to its advert and tracking blocking abilities – extremely useful on YouTube! It’s based on Chromium (like Google Chrome and Microsoft Edge), but more privacy/anti-ad orientated.

I’ve also been wanting to GPG/PGP sign some emails using my web based email clients so I’ve installed the Mailvelope plugin from the Google Chrome store and in conjunction with GPG4Win it means I should have access to all the PGP and GPG keys stored on my Windows 10 machine… Except it doesn’t work – it fails to list any installed keys… Why?

Well, it all comes down to a Chrome based protocol called NativeMessaging which requires software (such as GPG4Win) to registered their “acknowledgement” of browser plugins such as Mailvelope by adding (in the case of Windows) various registry settings for the browser to read and interlink.

In the case of Brave, it appears the others of GPG4Win aren’t (currently) aware of it and so don’t set the various registry settings for it to work correctly – and Brave, unlike Microsoft Edge, has no “fall back” facilities to check other browsers for their Native messaging setup. I have reported this to both the Brave Community and to GnuPG (the maintainers of GPG4Win) on their bug tracker – including suggested fixes for both organisations, but it may be some time before this is fixed. So what can you do in the meantime?

Easiest way:

If trust running random commands on your computer, run the following two commands in an escalated permissions (“Run as Administrator”) Windows Command Prompt to copy the existing settings from Chrome over:

REG COPY "HKCU\Software\Google\Chrome\NativeMessagingHosts\gpgmejson" "HKCU\Software\BraveSoftware\Brave-Browser\NativeMessagingHosts\gpgmejson" /s
REG COPY "HKLM\Software\Google\Chrome\NativeMessagingHosts\gpgmejson" "HKLM\Software\BraveSoftware\Brave-Browser\NativeMessagingHosts\gpgmejson" /s

Restart Brave and all should be working.

Manual way

Add/Set the following registry key:

Path: HKEY_CURRENT_USER\Software\BraveSoftware\Brave-Browser\NativeMessagingHosts\gpgmejson
Type: Reg_SZ
Data: C:\Program Files (x86)\Gpg4win\bin\gpgme-chrome.json

(updating the “Data” path to where you’ve installed Gpg4Win as appropriate)

Restart Brave and all should be working.

Windows 10 OpenSSH – Configuring Windows Git

This article is the last of a series I’ve written about migrating from using PuTTy on Windows to using the native OpenSSH client now available on Windows 10: you can read the rest of the articles via:

  1. Installation
  2. Storing keys using the SSH Agent
  3. Importing existing keys
  4. Creating a new public/private key pair
  5. Other useful OpenSSH commands
  6. Configuring Windows Git < You are here

If you are using Git for Windows and had previously been using PuTTy, you need to make a small tweak to the configuration for Git to use Windows 10’s OpenSSH client.

If you’ve been getting an error like:

FATAL ERROR: Disconnected: No supported authentication methods available (server sent: publickey)
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

when running git clone, but a test such as ssh git@github.com works, then you need to do the following steps.

  1. (Perhaps optional): Uninstall Git if you already have it installed. In theory, this can be done from Window’s “Add or Remove Programs”, but this was playing up for me. If you go into C:\Program Files\Git there should be a unins000.exe executable which will remove Git for you
  2. Install the latest version of Git (I actually uninstalled version 2.21.0.windows.1 and installed 2.23.0.windows.1) and during the setup, you’ll be prompt “Choosing the SSH executable”.
  3. Select “Use (Tortoise)Plink“, but enter in the path to Window’s OpenSSH SSH client: “c:\windows\system32\openssh\ssh.exeSelect (Tortoise)Plink and provide the path c:\windows\system32\openssh\ssh.exe to the Git setup
  4. Open a fresh PowerShell window and cloning should work!
    You might get a warning such as “warning: agent returned different signature type ssh-rsa (expected rsa-sha2-512)”, but that’s caused by a mismatch of keys and key types probably from the conversion from PuTTy keys. Generating a new public/private key pair and uploading that public key to Github/Bitbucket will fix that.

Windows 10 OpenSSH – Useful commands

This article is the fifth of a series I’ve written about migrating from using PuTTy on Windows to using the native OpenSSH client now available on Windows 10: you can read the rest of the articles via:

  1. Installation
  2. Storing keys using the SSH Agent
  3. Importing existing keys
  4. Creating a new public/private key pair
  5. Other useful OpenSSH commands < You are here
  6. Configuring Windows Git
Check the keys have been imported to the SSH agent
ssh-add -l

2048 SHA256:9hLEuBRdTBGDmAWxaDXhSwvqYLGVxDVtGiMumz9NUak C:\Users\userName/.ssh/id_rsa (RSA)
Show the public keys in the ssh-agent
ssh-add -L

ssh-rsa AAAAB3Nza...1F53nyTYMlmtcrZZp C:\Users\userName/.ssh/id_rsa
Delete all keys from the ssh-agent
ssh-add -D : hope you kept a backup!
Delete a specific key from the ssh-agent
ssh-add -d C:\Users\userName/.ssh/id_rsa