Retrieve Environment Variables into .txt File using Powershell

Here is a quick and simple one-liner to retrieve all of your environment variables into an output .txt file.

It utilizes the “gci env:” command to retrieve the environment variables on your current machine, then using piping, sends that information to “Format-List”, which will output all the properties retrieved with “gci env:” into a list of properties. Finally, the formatted list is piped into the “Out-File” command, to be saved as a .txt file at the given path.

(gci env:) | Format-List | Out-File -FilePath .\EnvironmentVariables.txt

For demonstration purposes, the -FilePath for the “Out-File” command is just the current location.

If you don’t want to output the environment variables into the text file, you can cut out the final piped command, and just use:

# with formatting:
(gci env:) | Format-List

# or without the formatting:
gci env:

This will just display the environment variable values in the terminal.

Check if two files are identical using Powershell

Checking whether two files are identical in Powershell is pretty straight forward. We will be making use of the MD5 hash of each file, and comparing their values together, to determine if the two files are the same.

We will utilize the “Get-FileHash” Powershell command, to retrieve each file’s hash, then compare them using the “-eq” command.

Here is an example below. We have two file paths stored in the variables, “$filePathOne” and “filePathTwo”, which we grab the MD5 hash from, and then comparing them together. The resulting boolean, indicating if the files are the same, is stored in the “$filesAreSame” variable.

$filePathOne = "C:\TestFileOne.png"
$filePathTwo = "C:\TestFileTwo.png"

$filesAreSame = (Get-FileHash $filePathOne -Algorithm MD5).Hash -eq (Get-FileHash $filePathTwo -Algorithm MD5).Hash

Start and Stop Android Emulator with Powershell

In order to programmatically start and stop an Android Emulator from Powershell, have a look at the Powershell script I’ve created below. This post assumes you already have the Android SDK, AVD and emulators set up on your machine. It is also assumed that you will only have one emulator instance at a time, to keep things simple.

# Variables
$emulatorExe = ".\emulator.exe"
$androidEmulator= "nexus_emulator"
$partitionSize = "512"
$emulatorPath = "C:\Program Files (x86)\Android\android-sdk\emulator\emulator.exe"

# START EMULATOR
Write-Host "Starting the Emulator..."
Start-Process -FilePath $emulatorPath -ArgumentList "-avd $androidEmulator -partition-size $partitionSize"

# DO STUFF HERE
Start-Sleep -s 10

# STOP EMULATOR
Write-Host "Stopping the Emulator..."
cmd /c "adb -e emu kill"

What this PS1 script will do is start up the emulator with the given variables:

  • $emulatorExe – The location of emulator.exe on your machine
  • $androidEmulator – Name of the emulator you want to open
  • $partitionSize – Internal storage allocated to your emulator
  • $emulatorPath – Path to where your emulator is located

It will then start up another process (outside of the current script process), for running the emulator with the provided variables:

Start-Process -FilePath $emulatorPath -ArgumentList "-avd $androidEmulator -partition-size $partitionSize"

The script will then sleep for 10 seconds before stopping the emulator. (Feel free to modify as you see fit, or perform actions between starting/stopping the emulator. This is only used as an example to show starting and stopping an emulator through powershell.

Afterwards, we will stop the emulator using the following command:

cmd /c "adb -e emu kill"

Programmatically starting/stopping an Android emulator is especially useful for test automation (UI Tests), when ran on build servers. You may want to look into setting this up in your project build script.