Category: PowerShell

PowerShell & Windows Admin Blog Archives, by Peter Whyte (SQL Database Administrator). Includes a lot of WSL posts & more…

  • How to Check Windows Firewall Status with PowerShell

    How to Check Windows Firewall Status with PowerShell

    This post will help you to check your local Windows Firewall status using PowerShell (whether it’s off or on for each Profile).

    PowerShell commands follow standards and use verb-noun pairs for cmdlets. The verb at the start of the command describes the action the cmdlet performs, and the noun part is the action being performed. Here’s a list of Common Verbs, another Microsoft Docs link. The place of truth as I call it.

    In this demo, we’re running Get-NetFirewallProfile with Format-Table
    We’re getting the Firewall Profile status, and also formatting it into a table after a pipe ‘|’ –

    # get local firewall status powershell
    Get-NetFirewallProfile | Format-Table Name, Enabled
    Get-NetFirewallProfile

    The Windows Firewall on this machine is enabled, for all Profiles <- have a look at this MS Docs link to know more about Windows Firewall Profiles.

    Internal follow-up links:
    # How To Disable Windows Firewall With PowerShell
    # How To Enable Windows Firewall With PowerShell

  • How to Enable Windows Firewall with PowerShell

    How to Enable Windows Firewall with PowerShell

    This post is contains a demo on how to enable Windows Firewall with PowerShell in Windows Server.

    This might be needed if you have discovered Windows Firewall is disabled on your or multiple computers, or you might want to re-enable it after disabling this for a test (see my other post on disabling instead of enabling).

    Enabling Windows Firewall with PowerShell can be done by going through the following steps:

    # Understand Windows Firewall Profiles
    # Get-NetFirewallProfile PowerShell
    # Set-NetFirewallProfile PowerShell

    Understand Windows Firewall Profiles

    Before running any command or script on your machine, it’s important for us to understand everything that it’s doing. In the command below we are disabling all 3 profiles:

    # Domain – For when you are connected to a Domain Controller (computer connected to AD).
    # Private – For your home or private networks.
    # Public – For public WIFI locations such as coffee shops etc.

    For more information on this, see this link – Microsoft Docs: Windows Firewall Profiles

    Also remember, all of this can be viewed and changed via GUI to help with understandings >

    Enable Windows Firewall

    Get-NetFirewallProfile PowerShell

    PowerShell commands follow standards and use verb-noun pairs for cmdlets. The verb at the start of the command describes the action the cmdlet performs, and the noun part is the action being performed. Here’s a list of Common Verbs, another Microsoft Docs link. The place of truth as I call it.

    In this demo, we’re running Get-NetFirewallProfile with Format-Table
    We’re getting the Firewall Profile status, and also formatting it into a table after a pipe ‘|’ –

    # get local firewall status
    Get-NetFirewallProfile | Format-Table Name, Enabled
    Get-NetFirewallProfile

    In the example above, the Windows Firewall is disabled, showing as ‘False‘.

    Set-NetFirewallProfile PowerShell

    We’re switching the cmdlet we ran above from Get to Set here (following on from verb-noun cmdlet descriptions).

    To be able to run this; we have to open PowerShell or Windows Terminal as Administrator.

    Set-NetFirewallProfile is being executed below, which enables all Profiles of the Local Windows Firewall –

    # disable local firewall
    Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True
    
    # get local firewall status
    Get-NetFirewallProfile | Format-Table Name, Enabled
    Set-NetFirewallProfile

    I followed the change by another run of Get-NetFirewallProfile to verify the change.

  • How to Disable Windows Firewall with PowerShell

    How to Disable Windows Firewall with PowerShell

    This post contains a demo on how to disable Windows Firewall with PowerShell on a Windows computer.

    Disabling the Local Firewall on Windows is not a recommended move, and it will ultimately make your computer less secure. Sometimes it’s necessary though, for example, during troubleshooting a difficult issue you might want to disable it for a quick test to rule the FW out as being the root cause. But in this example, you will have already tried to ensure the Firewall Rule exists before turning it off completely.

    Anyway, disabling Windows Firewall with PowerShell can be done by going through the following steps:

    # Understanding Firewall Profiles in Windows
    # Get Status of Windows Firewall with PowerShell
    # Disable Windows Firewall with PowerShell

    Understanding Firewall Profiles in Windows

    Before running any command or script on your machine, it’s important for us to understand everything that it’s doing. In the command below we are disabling all 3 profiles:

    # Domain – For when you are connected to a Domain Controller (computer connected to AD).
    # Private – For your home or private networks.
    # Public – For public WIFI locations such as coffee shops etc.

    For more information on this, see this link – Microsoft Docs: Windows Firewall Profiles

    Also remember, all of this can be viewed and changed via GUI to help with understandings >

    Disable Windows Firewall via GUI

    Get Status of Windows Firewall with PowerShell

    PowerShell commands follow standards and use verb-noun pairs for cmdlets. The verb at the start of the command describes the action the cmdlet performs, and the noun part is the action being performed. Here’s a list of Common Verbs, another Microsoft Docs link. The place of truth as I call it.

    In this demo, we’re running Get-NetFirewallProfile with Format-Table
    We’re getting the Firewall Profile status, and also formatting it into a table after a pipe ‘|’ –

    # get local firewall status
    Get-NetFirewallProfile | Format-Table Name, Enabled
    Get-NetFirewallProfile

    Disable Windows Firewall with PowerShell

    We’re switching the cmdlet we ran above from Get to Set here (following on from verb-noun cmdlet descriptions).

    To be able to run this; we have to open PowerShell or Windows Terminal as Administrator.

    Set-NetFirewallProfile is being executed below, which disables all Profiles of the Local Windows Firewall –

    # disable local firewall ps1
    Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False
    
    # get local firewall status ps1
    Get-NetFirewallProfile | Format-Table Name, Enabled
    Set-NetFirewallProfile

    I followed the change by another run of Get-NetFirewallProfile to verify the change.

    If this is a temporary change for you, to re-enable the Windows Firewall, amend the ‘-Enabled False‘ statement to ‘-Enabled True‘.

  • Best Terminal for Windows 2022

    Best Terminal for Windows 2022

    This is a post to share an opinion on the best Terminal for Windows 2022. The answer to all our needs in the year 2022 is Windows Terminal.

    I have a blog post on How to Install Windows Terminal, and here is a link to the main Windows Terminal GitHub repo. You’ll find more up-to-date Windows Terminal information by following the GitHub link. This guide is a personal post to share a random thought.

    That’s it. Done. Nothing much else to know about CLI Terminals in Windows. No need to be looking at other Windows Terminals in 2022, especially if you’re learning the ropes.

    I’ll be honest, my reason for posting this one is to see how it performs against other websites for the term “Best Terminal for Windows”. It’s crazy, every post has way too much information – here are a billion different types of terminals in Windows. Download and try them all, even if you’re a novice.

    Best Terminal for Windows 2022
    “best terminal for windows” – Google Search, August 2022

    Every one of them is over the top, all done for the Google SEO Ranking points that you get for writing 10,000 words in a post for a target keyword.

    But… if you’re up for exploring random text editors that some folk think makes a good Windows Terminal, then go ahead.

    Best Terminal for Windows

    The best Terminal in Windows to use is Windows Terminal, as mentioned at the top of this post.

    This is the best terminal to get you started if you’re committed to using the Windows OS. Anything else is exploratory. The best way to approach software in my opinion is, to choose the default vendor first. In this case, and in most of my work it’s Microsoft. So go try out the software that Microsoft is developing first if you are on a Windows OS, and if it’s not working to your needs, try to find an alternative.

    In Windows Terminal, we can have multiple tabs for whichever terminal session need. One of the best examples is WSL –

    wsl windows terminal

    This is a very different post from what I usually do. But it’s good to share an honest straight-to-post thought. I intentionally don’t give much personality away on this blog. It’s a place for posting my technical notes, with the hope that it provides every viewer with what they were searching for Online.

    At least this top Terminal for Windows list comes from someone in the software/database engineering game. I use Windows Terminal every day and love it. Also, this post will likely & intentionally get lost in the nether. As ever, see how goes.

    That’s me for now, over. This is the post you need to truly understand what the Best Terminal for Windows is, I’m sure you’ll agree.

  • PowerShell Create Folder If Not Exists

    PowerShell Create Folder If Not Exists

    In this post, I share a script that will help you create folders and sub-folders with PowerShell if they do not already exist. We use Test-Path in our PS scripts to check if objects exist before executing the create command.

    This is one small part of a more detailed blog post I have on creating files and folders using PowerShell. My other post, How to Create New Files & Folders in PowerShell also covers more details on the differences between creating files vs creating folders.

    Create New Folder (if not exists) in PowerShell

    We’re creating a new folder, only if it doesn’t already exist – “C:\temp\demo”

    The create folder command is New-Item, which runs conditionally depending on the Test-Path true/false result. When we run this script it also creates your C:\temp directory if it doesn’t already exist.

    # create folder if not exists .ps1
    $path = "c:\temp\demo"
    If(!(Test-Path $path) ){
        New-Item -ItemType Directory -Force -Path $path
    }

  • How to Create New Files & Folders in PowerShell

    How to Create New Files & Folders in PowerShell

    This is a post on how to create new files and folders using PowerShell.

    Creating new files and folders in Windows is generally done via GUI. That’s what it’s there for, the simplicity. But when you’re scripting or doing admin work, you might want to create new files and folders via command.

    This guide covers performing the following in your PowerShell Terminal –

    # PowerShell: Create a New Folder
    # PowerShell: Create a New File
    # PowerShell: Create New Folder (if not exists)

    Create New Folder in Powershell

    New-Item is the command to create the new folder and item. We just need to amend the ItemType to be a Directory for us to create a folder.

    # create new folder in powershell
    New-Item -ItemType Directory -Name Test_Stuff
    PowerShell New-Item Directory

    Create New File in PowerShell

    As mentioned above, it’s the same command, New-Item, and we’re changing the ItemType to File this time.

    # create new file in powershell
    New-Item -ItemType File -Name Test_File.txt
    PowerShell New-Item File

    Further reading: We also can add text to this file by using Add-Content & Get-Content (to view content).

    Create New Folder (if not exists) in PowerShell

    This time, it’s a script rather than a one-liner/cmdlet.

    We’re only creating a new folder only if it doesn’t already exist – “C:\temp\demo”

    When we run this script it also creates the C:\temp directory if it doesn’t already exist.

    The create folder command is New-Item, which runs conditionally depending on the Test-Path true/false result.

    # create folder if not exists
    $path = "c:\temp\demo"
    If(!(Test-Path $path) ){
        New-Item -ItemType Directory -Force -Path $path
    }
    PowerShell Create Folder if not exists
  • How To Change From WSL2 to WSL1

    How To Change From WSL2 to WSL1

    This is a post on how to change a Windows Subsystem for Linux (WSL) Distribution from running on Version 2 to Version 1.

    WSL has two versions, the latest (2), and the original version (1). You can have many different Linux Distributions installed on your machine, and you can easily change the WSL Version for one of them.

    This Microsoft documentation page explains the key differences between versions and will help explain why you might want to downgrade back to version 1 for your particular WSL development needs.

    To change one of your WSL Distributions from WSL2 to WSL1, we just need to pass in the –set-version parameter and it’ll be converted very quick. I’m listing my available WSL Distro’s before and after to visually verify the change.

    # check wsl distro versions
    wsl -l -v
    
    # change wsl distro using name above to desired version
    wsl --set-version Ubuntu 1
    
    WSL Downgrade WSL2 to WSL1

  • How to Reboot a WSL Distribution/Instance

    How to Reboot a WSL Distribution/Instance

    This is a post on how to reboot a Windows Subsystem for Linux (WSL) virtual machine environment on your local machine. I’ve needed to do this a few times in the past year, my WSL instance throws some random error which was only resolved after a reboot.

    As far as I know, we are unable to reboot from within WSL due to the lack of systemd. There are some open-source projects in the works to enable this which I’ve not tried yet. So we need to run a simple ‘wsl’ command to shutdown the host, and the enter WSL to get it back online.

    This example shows exactly that, and I’m listing my installed WSL Distributions between each step.

    # check wsl version
    wsl -l -v
    
    # reboot wsl2 instance
    wsl --shutdown
    # enter default wsl instance to boot
    wsl

    If you have multiple Linux Distributions installed on your Windows machine, by default you can only have one of them running, so when you’re switching between using ‘wsl -d <instanceName>’ the other will go offline. There is a way to get multiple WSL instances running at the same time, even with a GUI – have a look at this WSL Distro Manage project in GitHub if of interest.

    One last note, the LXSS Manager Service is the broker to the Linux subsystem driver. We can also restart this service or of course reboot our local machine.

  • How to Check WSL Versions

    How to Check WSL Versions

    WSL allows users to develop and run applications within a GNU/Linux environment on a Windows computer. Previous to this Windows Feature you might have needed VM’s or a dual boot setup for this type of test/development environment.

    There are 2 versions of WSL, the old (1), and the new (2). We should ideally be using the latest version like all other software in existence, but there are use cases where we might prefer to use WSL Version 1. This Microsoft documentation page explains the key differences between versions, and this post is to help explain how to check your WSL Version.

    Personally I prefer to use WSL Version 2 as we’re able to make use of more apps like Docker. Also, I don’t need to worry about the exceptions for when it’s best to use WSL Version 1.

    With descriptions out of the way, we can check our WSL Versions by running the following from a PowerShell Terminal –

    # check wsl version
    wsl -l -v

    The above gives us a list of all installed Linux Distributions on your local machine including WSL Versions.

  • How to Manage S3 Buckets with AWS CLI

    How to Manage S3 Buckets with AWS CLI

    This is a post on how to help manage S3 Buckets with AWS CLI, and to help explain some local Operating System (OS) considerations when running such commands.

    First of all, you will need to be authenticated to your AWS Account and have AWS CLI installed. I cover this in previous blog posts:
    # How to Install and Configure AWS CLI on Windows
    # How to Install and Configure AWS CLI on Ubuntu

    I’m more often involved in the PowerShell side rather than Linux. AWS CLI commands do the same thing in both environments, but the native (OS) language is used around it for manipulating data for output and for other things like wrapping commands in a For Each loop. All commands in this post can run on either OS.

    PowerShell is cross-platform and has supported various Linux & DOS commands since its release. Some are essential for everyday use, for example, ping, cd, ls, mkdir, rm, cat, pwd and more. There are more commands being added over time like tar and curl which is good to see. Plus, we have WSL to help integrate non-supported Linux commands.

    Here’s one of the simplest examples which list all S3 buckets the executing IAM User owns within your AWS Account.

    # List all buckets in an AWS Account
    aws s3 ls
    AWS S3 List Buckets

    The Default Region is configured during the AWS CLI Configuration as linked above. We can change this by running aws configure set region or configure your Environment Variables. Alternatively, we can pass in the –Region variable after ‘ls’ in the command to get a specific Region’s S3 Buckets. There are more ways for us to run commands across multiple Regions which I hope to cover another day.

    Now I’m going to run a command to show me the Access Control List (ACL) of the bucket, using the list of Bucket Names I ran in the previous command. This time, I’m utilising the s3api command rather than s3 – look here for more information on the differences between them. When running AWS CLI commands these API docs will always help you out.

    # Show S3 Bucket ACL
    aws s3api get-bucket-acl --bucket my-bucket

    Next up, I’m going to create a bucket using the s3 command rather than s3api. The reason I’m doing this is, I want to rely on my Default Region for the new S3 Bucket, rather than specifying it within the command. Here’s AWS’s explanation of this –

    “Regions outside of us-east-1 require the appropriate LocationConstraint to be specified in order to create the bucket in the desired region – “
    –create-bucket-configuration LocationConstraint=eu-west-1
    AWS API Docs

    The following command is creating a new S3 Bucket in my Default Region and I’m verifying the location with get-bucket-location afterwards.

    # Change AWS CLI Default Region
    aws configure set region eu-west-1
    
    # Create a new S3 Bucket in your Default Region
    aws s3 mb s3://pw-blog-bucket-101
    
    # Check the Region of a S3 Bucket
    aws s3api get-bucket-location --bucket pw-blog-bucket-101 --output text

    And finally, to finish this off I’m going to:
    – Create a folder (known as Object) within the new Bucket.
    – List items in the S3 Bucket.
    – Copy a file from my desktop into the folder.
    – List items in the S3 Bucket.
    – Delete the Bucket.

    # Create folder/object within a S3 Bucket
    aws s3api put-object --bucket pw-blog-bucket-101 --key folder1/
    
    # Show objects within S3 Bucket
    aws s3 ls s3://pw-blog-bucket-101 --recursive --human-readable
    
    # Copy a local file into the folder above
    aws s3 cp .\Upload2S3.txt s3://pw-blog-bucket-101/folder1
    
    # Show objects within S3 Bucket
    aws s3 ls s3://pw-blog-bucket-101 --recursive --human-readable
    
    # Delete the S3 Bucket
    aws s3 rb s3://pw-blog-bucket-101
    
    # List the S3 Bucket above (expect error)
    aws s3 ls s3://pw-blog-bucket-101