Tag: macOS

Getting started and Learn PowerShell on Microsoft Learn

Getting started and Learn PowerShell on Microsoft Learn!

I remember running my first commands and building my first automation using Windows PowerShell back in 2006. Since then, PowerShell became one of my daily tools to build, deploy, manage IT environments. With the release of PowerShell version 6 and now PowerShell 7, PowerShell became cross-platform. This means you can now use it on even more systems like Linux and macOS. With PowerShell becoming more and more powerful (you see what I did there ;)), more people are asking me how they can get started and learn PowerShell. Luckily we just released 5 new modules on Microsoft Learn for PowerShell.

Microsoft Learn Introduction to PowerShell

Learn about the basics of PowerShell. This cross-platform command-line shell and scripting language is built for task automation and configuration management. You’ll learn basics like what PowerShell is, what it’s used for, and how to use it.

Learning objectives

  • Understand what PowerShell is and what you can use it for.
  • Use commands to automate tasks.
  • Leverage the core cmdlets to discover commands and learn how they work.

PowerShell learn module: Introduction to PowerShell

Connect commands into a pipeline

In this module, you’ll learn how to connect commands into a pipeline. You’ll also learn about filtering leftformatting right, and other important principles.

Learning objectives

  • Explore cmdlets further and construct a sequence of them in a pipeline.
  • Apply sound principles to your commands by using filtering and formatting.

PowerShell learn module: Connect commands into a pipeline

Introduction to scripting in PowerShell

This module introduces you to scripting with PowerShell. It introduces various concepts to help you create script files and make them as robust as possible.

Learning objectives

  • Understand how to write and run scripts.
  • Use variables and parameters to make your scripts flexible.
  • Apply flow-control logic to make intelligent decisions.
  • Add robustness to your scripts by adding error management.

PowerShell learn module: Introduction to scripting in PowerShell

Write your first PowerShell code

Getting started by writing code examples to learn the basics of programming in PowerShell!

Learning objectives

  • Manage PowerShell inputs and outputs
  • Diagnose errors when you type code incorrectly
  • Identify different PowerShell elements like cmdlets, parameters, inputs, and outputs.

PowerShell learn module: Write your first PowerShell code

Automate Azure tasks using scripts with PowerShell

Install Azure PowerShell locally and use it to manage Azure resources.

Learning objectives

  • Decide if Azure PowerShell is the right tool for your Azure administration tasks
  • Install Azure PowerShell on Linux, macOS, and/or Windows
  • Connect to an Azure subscription using Azure PowerShell
  • Create Azure resources using Azure PowerShell

PowerShell learn module: Automate Azure tasks using scripts with PowerShell

Conclusion

I hope this helps you get started and learn PowerShell! If you have any questions feel free to leave a comment!



Store Secret in PowerShell SecretStore

Stop typing PowerShell credentials in demos using PowerShell SecretManagement

We all sometimes create presentations with some PowerShell demos. And often, we need to use credentials to log in to systems for example PowerShell when delivering these presentations. This can lead that we don’t use very strong passwords because we don’t want to type them during a presentation, you see the problem? So, here is how you can use the PowerShell SecretManagement and SecretStore modules to store your demo credentials on your machine.

Doing this is pretty simple:

Install the SecretManagement and SecretStore PowerShell modules.

Install-Module Microsoft.PowerShell.SecretManagement, Microsoft.PowerShell.SecretStore

Register a SecretStore to store your passwords and credentials. I this example we are using a local store to do that. Later in this blog post, we will also have a look at how you can use Azure Key Vault to store your secrets. This is handy if you are working on multiple machines.

Register-SecretVault -Name SecretStore -ModuleName Microsoft.PowerShell.SecretStore -DefaultVault

Now we can store our credentials in the SecretStore. In this example, I am going to store the password using, and I will add some non-sensitive data as metadata to provide some additional description.

Set-Secret -name DemoAdmin01 -Secret "demoAdmin01PassWord" -Metadata @{demo = "My Hyper-V demo for Windows Server and Linux Remoting"}

You can also store other data types like PSCredential in SecretManagement. For this example, I am using just a simple string, because this works for most scenarios. 

PowerShell SecretManagement supports the following secret data types:

  • byte[]
  • string
  • SecureString
  • PSCredential
  • Hashtable
Store Secret in PowerShell SecretStore
Store Secret in PowerShell SecretStore

Now you can start using this secret in the way you need it. In my case, it is the password of one of my admin users. 

$DemoAmdin01secret = Get-Secret -Vault SecretStore -Name DemoAdmin01
$DemoAmdin01Cred = New-Object -TypeName PSCredential -ArgumentList "DemoAdmin01", $DemoAmdin01secret

As mentioned before, you can also store a PSCredential object directly in SecretManagement if that makes it easier for you.

These two lines, I could also store in my PowerShell profile I use for demos, or in my demo startup script. In this case, the credential object is available for you to use.

Use Secret Store Credentials
Use Secret Store Credentials

If you are using multiple machines and you want to keep your passwords in sync, the Azure Key Vault extension.

Install-Module Az.KeyVault
Register-SecretVault -Module Az.KeyVault -Name AzKV -VaultParameters @{ AZKVaultName = $vaultName; SubscriptionId = $subID}

Now you can store and get secrets from the Azure Key Vault and you can simply use the -Vault AzKV parameter instead of -Vault SecretStore. 

I hope this blog provides you with a short overview of how you can leverage PowerShell SecretManagement and SecretStore, to store your passwords securely. If you want to learn more about SecretManagement check out Microsoft Docs.

I also highly recommend that you read @Pierre Roman blog post on leveraging PowerShell SecretManagement to generalize a demo environment.



Get System Uptime with PowerShell

Get System Uptime with PowerShell

Sometimes you want to know how long your system is running. There are multiple ways to get the uptime of your system using the GUI or command line. PowerShell also offers a simple way to get the system uptime. If you are running PowerShell 6 or PowerShell 7 you can get the uptime with this simple cmdlet, which works on Windows, Linux, and macOS.

To get the system uptime on Windows, Linux, or macOS using PowerShell, you can use the Get-Uptime cmdlet.

Get-Uptime

Here how that looks on Ubuntu:

Get System Uptime on Linux using PowerShell

You can also use the “-Since” parameter to return a DateTime object representing the last time that the operating system was booted.

Get-Uptime PowerShell cmdlet with -since parameter

If you are running Windows PowerShell 5 or older, you can use the CIM Instance method and the Win32_OperatingSystem class. The Win32_OperatingSystem class has a LastBootupTime property which shows the time when the computer was rebooted.

Get-CimInstance -ClassName Win32_OperatingSystem | Select LastBootUpTime

I hope that quick PowerShell post gives you an overview of how you can get the system uptime on Windows, Linux, and macOS using PowerShell. If you have any questions feel free to leave a comment. If you want to know more about how you can install and update to PowerShell 7, check out my blog post, you can also learn what is new in PowerShell 7 right here.

You can also learn about PowerShell remoting between Windows, Linux, and macOS using PowerShell remoting over SSH.



PowerShell Remoting over SSH in PowerShell 7

Enable PowerShell SSH Remoting in PowerShell 7

In this blog post, we will have a look at how you can enable and set up PowerShell SSH Remoting or PowerShell Remoting over SSh with PowerShell 7. With PowerShell Core 6, Microsoft introduced PowerShell 7 Remoting over SSH, which allows true multiplatform PowerShell remoting between Linux, macOS, and Windows. PowerShell SSH Remoting creates a PowerShell host process on the target machine as an SSH subsystem. Normally, Windows PowerShell remoting uses WinRM for connection negotiation and data transport. However, WinRM is only available on Windows-based machines.

There are also some downsides to it. SSH-based remoting doesn’t currently support remote endpoint configuration and JEA (Just Enough Administration). It is also important to understand that this is not just another PowerShell SSH client.

Use SSH Transport with PowerShell Remoting

To use PowerShell 7 remoting with SSH on Windows, Linux, and macOS machines, you can use the same cmdlets you are already familiar from Windows PowerShell remoting with WinRM.

  • New-PSSession
  • Enter-PSSession
  • Invoke-Command

There are three new parameters for these cmdlets if you are using PowerShell SSH remoting.

  • -HostName (Instead of -Computername, you define the SSH target)
  • -UserName (Instead of -Credentials you use the -UserName parameter)
  • -KeyFilePath (If you are using SSH key authentication you can use the -KeyFilePath parameter to point to the key file)
New-PSSession -HostName tomsssh.server.com -UserName thomas


What's new in PowerShell 7

What’s new in PowerShell 7

As you know, the PowerShell team just released the new PowerShell version called PowerShell 7. PowerShell 7 will brings a couple of new features and enhancements not only for users who used PowerShell Core 6 but also for people who are currently using Windows PowerShell 5.0. I want to quickly give you an overview of what’s new in PowerShell 7.

New feature enhancements – What’s new in PowerShell 7

There are many enhancements in PowerShell 7 to make it the best PowerShell version yet. It is almost impossible to list all of them in one single blog post, so I will focus on the once, which are the most important once for me.

Out-GridView, -ShowWindow and other GUI cmdlets are back on Windows

With .NET Core 3 brining back WPF support on Windows, the PowerShell team was able to bring back some of the popular graphical tools and cmdlets like Out-Gridview, Show-Command, and the Get-Help -ShowWindow.

Show-Command PowerShell 7

Show-Command PowerShell 7

ForEach-Object -Parallel

Since I started using PowerShell and get into working with objects, ForEach-Object was one of the most critical cmdlets. By adding the -Parallel parameter to the ForEach-Object cmdlet, you can execute a scriptblock in parallel, similar to the option we had with PSWorkflow. With that performance can get so much better if you are working with a broad set of objects. You can also optionally set the maximum threads, which will be used in parallel (the default thread count is set to 5) with the -ThrottleLimit parameter.



PowerShell 7 Installer

How to Install and Update PowerShell 7

The PowerShell team just announced PowerShell 7. PowerShell 7 is built on .NET Core 3 and brings back many APIs required by modules built on .NET Framework so that they work with .NET Core runtime. While PowerShell Core 6 was focusing on bringing cross-platform compatibility, PowerShell 7 will focus on making it a viable replacement for Windows PowerShell 5.1 and bringing near parity with Windows PowerShell. Here is how you can install and update PowerShell 7 on Windows and Linux using a simple one-liner.

If you want to know what’s new in PowerShell 7, check out my blog post!

One great example of how cross-platform PowerShell can work, check out my blog post: How to set up PowerShell SSH Remoting.

Install PowerShell 7

Before showing you the one-liner option to install PowerShell 7, I want to share with you the documentation to install PowerShell 7 on different operating systems like Windows, macOS, and Linux.

Install from the Microsoft Store

If you are running Windows 10, you can easily install PowerShell 7 and higher through the Microsoft Store.

Download and install PowerShell in the Microsoft Store
Download and install PowerShell in the Microsoft Store

You can search it in the Microsoft Store app on Windows 10 or you can simply use this link.

Install PowerShell 7 using winget

If you are running Windows 10, and you have a new command-line tool called winget. The winget command line tool enables you to easily discover, install, upgrade, remove and configure applications on Windows 10 computers.

PowerShell Winget
PowerShell Winget

You can simply type the following command, to get the latest version of PowerShell.

winget install PowerShell

Later you will also be able to automate the process to download apps from the Windows Store using Winget.

One-liner to install or update PowerShell 7 on Windows 10

Steve Lee (Microsoft Principal Software Engineer Manager in the PowerShell Team) shared some one-liner, which helps you quickly install and update:

Install and Update PowerShell 7

You can use this single command in Windows PowerShell to install PowerShell 7.

iex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI"

There are additional switches to, for example, install daily builds of the latest PowerShell previews.

-Destination
The destination path to install PowerShell Core to.

-Daily
Install PowerShell Core from the daily build.
Note that the ‘PackageManagement’ module is required to install a daily package.

-Preview
Install the latest preview, which is currently version 7.

-UseMSI
Use the MSI installer.

-Quiet
The quiet command for the MSI installer.

-DoNotOverwrite
Do not overwrite the destination folder if it already exists.

-AddToPath
On Windows, add the absolute destination path to the ‘User’ scope environment variable ‘Path’;
On Linux, make the symlink ‘/usr/bin/pwsh’ points to “$Destination/pwsh”;
On MacOS, make the symlink ‘/usr/local/bin/pwsh’ points to “$Destination/pwsh”.

One-liner to install or update PowerShell 7 on Linux

Install PowerShell 7 on Linux

You can use this as a single command to install PowerShell 7 on Linux.

wget https://aka.ms/install-powershell.sh; sudo bash install-powershell.sh; rm install-powershell.sh

Depending on your distro you are using, this will register Microsoft’s pkg repos and install that package (deb or rpm).

You can also use the following switches:

-includeide
Installs VSCode and VSCode PowerShell extension (only relevant to machines with a desktop environment)

-interactivetesting
Do a quick launch test of VSCode (only applicable when used with -includeide)

-skip-sudo-check
Use sudo without verifying its availability (hard to accurately do on some distros)

-preview
Installs the latest preview release of PowerShell side-by-side with any existing production releases

To currently run the PowerShell Preview, you can run the following command:

pwsh

After Installing

After you have installed PowerShell 7, also make sure to update PowerShellGet and the PackageManagement module.

You might ask, what my preferred way is? On Windows 10 I am definitely using the Microsoft Store and with winget I can easily do that from the command line. I hope this blog post was helpful. If you have any questions, please let me know in the comments.



How to Install AzCopy

How to Install AzCopy for Azure Storage

AzCopy is a command-line tool to manage and copy blobs or files to or from a storage account. It also allows you to sync storage accounts and move files from Amazon S3 to Azure storage. In this blog post, I will cover how to install AzCopy on Windows, Linux, macOS, or in update the version in the Azure Cloud Shell.

AzCopy v10 is now generally available to all of our customers and provides higher throughput and more efficient data movement compared to the earlier version of AzCopy (v8). Version 10 also adds additional functionality like sync of blob storage accounts and much more.

Install AzCopy

You can get the latest version of AzCopy from here: Get started with AzCopy

Install AzCopy on Windows

To install AzCopy on Windows, you can run the following PowerShell script, or you can download the zip file and run it from where ever you want. This script will add the AzCopy folder location to your system path so that you can run the AzCopy command from anywhere.

 
#Download AzCopy
Invoke-WebRequest -Uri "https://aka.ms/downloadazcopy-v10-windows" -OutFile AzCopy.zip -UseBasicParsing
 
#Curl.exe option (Windows 10 Spring 2018 Update (or later))
curl.exe -L -o AzCopy.zip https://aka.ms/downloadazcopy-v10-windows
 
#Expand Archive
Expand-Archive ./AzCopy.zip ./AzCopy -Force
 
#Move AzCopy to the destination you want to store it
Get-ChildItem ./AzCopy/*/azcopy.exe | Move-Item -Destination "C:\Users\thmaure\AzCopy\AzCopy.exe"
 
#Add your AzCopy path to the Windows environment PATH (C:\Users\thmaure\AzCopy in this example), e.g., using PowerShell:
$userenv = [System.Environment]::GetEnvironmentVariable("Path", "User")
[System.Environment]::SetEnvironmentVariable("PATH", $userenv + ";C:\Users\thmaure\AzCopy", "User")

Install AzCopy on Linux

To install AzCopy on Linux, you can run the following shell script, or you can download the tar file and run it from where ever you want. This script will put the AzCopy executable into the /usr/bin folder so that you can run it from anywhere.

 
#Download AzCopy
wget https://aka.ms/downloadazcopy-v10-linux
 
#Expand Archive
tar -xvf downloadazcopy-v10-linux
 
#(Optional) Remove existing AzCopy version
sudo rm /usr/bin/azcopy
 
#Move AzCopy to the destination you want to store it
sudo cp ./azcopy_linux_amd64_*/azcopy /usr/bin/

Authorize with Azure Storage

When you start working with Azure Storage, you have two options to authorize against the Azure Storage. You can provide authorization credentials by using Azure Active Directory (AD), or by using a Shared Access Signature (SAS) token.

It also depends on which services you want to use.

Storage typeSupported method
Blob storageAzure AD and SAS
Blob storage (hierarchical namespace)Azure AD
File storageSAS only

Authenticate using Azure AD

To authenticate with AzCopy using Azure AD, you can use the following command

 
azcopy login

Authenticate using SAS token

To authenticate with AzCopy using a SAS token you can use this command as an example

 
azcopy cp "C:\local\path" "https://account.blob.core.windows.net/mycontainer1/?sv=2018-03-28&ss=bjqt&srt=sco&sp=rwddgcup&se=2019-05-01T05:01:17Z&st=2019-04-30T21:01:17Z&spr=https&sig=MGCXiyEzbtttkr3ewJIh2AR8KrghSy1DGM9ovN734bQF4%3D" --recursive=true

To make things easier you can use Azure PowerShell to generate the SAS token for you. I wrote a blog post on ITOPSTALK.com about how you can do that. You can get the SAS token using the following Azure PowerShell command. If you are running Linux or macOS, you can find on this blog post, how to install PowerShell 6.

 
Connect-AzAccount
Get-AzSubscription
 
$subscriptionId = "yourSubscriptionId"
$storageAccountRG = "demo-azcopy-rg"
$storageAccountName = "tomsaccount"
$storageContainerName = "images"
$localPath = "C:\temp\images"
 
Select-AzSubscription -SubscriptionId $SubscriptionId
 
$storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $storageAccountRG -AccountName $storageAccountName).Value[0]
 
$destinationContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
 
$containerSASURI = New-AzStorageContainerSASToken -Context $destinationContext -ExpiryTime(get-date).AddSeconds(3600) -FullUri -Name $storageContainerName -Permission rw
 
azcopy copy $localPath $containerSASURI --recursive

To learn more about SAS tokens, check out Using shared access signatures (SAS).

I hope this helps you to install AzCopy and configure it. If you have any questions, feel free to leave a comment.



Install or Update PowerShell 6 on Windows 10

How to Install and Update PowerShell 6

Today Windows 10 and Windows Server 2019 ship with Windows PowerShell 5.1 as the default version. PowerShell Core 6 is a new edition of PowerShell that is cross-platform (Windows, macOS, and Linux), open-source, and built for heterogeneous environments and the hybrid cloud. PowerShell 6 today is a side by side version next to Windows PowerShell 5.1. That means on Windows you cannot just upgrade to PowerShell 6, you will need to install it, same as on Linux and macOS. This blog post shows you how simple you can install PowerShell 6 or update PowerShell 6, if you have already installed it, on Windows 10, Windows Server 2019 or Linux.

One great example of how cross-platform PowerShell can work, check out my blog post: How to set up PowerShell SSH Remoting

Of course, you can find excellent documentation out there on Microsoft Docs. However, Steve Lee (Microsoft Principal Software Engineer Manager in the PowerShell Team) shared some one-liner, which helps you quickly install and update PowerShell 6.

Install PowerShell Core 6

Before showing you the one-liner option to install PowerShell 6, I want to share with you the documentation to install PowerShell Core 6 on different operating systems like Windows, macOS, and Linux.



PowerShell Core

PowerShell Core 6.0 for Windows, Linux and macOS General Available (GA)

Microsoft yesterday announced the general availability of PowerShell Core 6.0. PowerShell Core 6.0 is a new edition of PowerShell that is cross-platform (Windows, macOS, and Linux), open-source, and built for heterogeneous environments and the hybrid cloud. PowerShell Core uses .NET Core 2.0 as its runtime. PowerShell Core also exposes the API set offered by .NET Core 2.0 to be used in PowerShell cmdlets and scripts.

You can find more about how to install  and update PowerShell 6 on my blog.

First and foremost, thank you to all of our amazing community, especially our open-source contributors (the most recent of which you can find on our community dashboard at https://aka.ms/PSGitHubBI) for donating your time and energy to PowerShell Core. Whether you contributed code, tests, documentation, issues, or even just your feedback and opinions, we are extremely grateful for the sweat and tears that you’ve invested in PowerShell. (For those interested in contributing, hop and over to our Contribution Guide on GitHub. You don’t have to be a guru to help out!)

Quote: PowerShell Team Blog: PowerShell Core 6.0: Generally Available (GA) and Supported!

PowerShell Core Installer

You can get PowerShell Core 6.0 in the links below, or you can also check out my blog post about how to install PowerShell 6.

PowerShell Core is open source and you can have a look at the code on PowerShell on GitHub. I hope this article gives you an overview about the new PowerShell Core 6.0. If you have any questions, feel free to leave a comment.



Surface Precisiaon Mouse Box

Microsoft Surface Precision Mouse Review

Last week I got the Microsoft Surface Precision Mouse delivered. The Surface Precision Mouse will become the replacement mouse for my Microsoft Sculpt Mouse, which I used for the last couple of years. Now I want to give you some impressions about the Surface Precision Mouse, since I am very happy and very surprised about the feature set.

Surface Precision Mouse

First of all the Microsoft Surface Precision Mouse feels awesome, very high quality and it is very comfortable in your hand. It feels very precise and you get very quickly used to it.

Now let me write about some extra features you might didn’t know about. You first get a button to configure the scroll wheel in two different speeds, one feels very light and fast and the other one is slower and is more resistant, so you can choose what you like more and switch between them, depending on your task.

Microsoft Surface Percision Mouse Settings

Of course you get some extra buttons which you can customize with different shortcuts, for example to open the Windows 10 Tasks View or other applications. You can also customize the buttons depending on the application you are working with.

Surface Precision Mouse Bottom

By already having the perfect mouse, in terms of feeling, comfort, precision and customizability, you get a really cool extra feature. The Surface Precision Mouse gives you more multi-tasking power by allowing you to work seamlessly across up to three computers, supports both Bluetooth and wired USB connections. You can pair your Surface Precision Mouse with three different devices and you can manually switch between them with the button on the bottom of the mouse, or you can use something called Smart Switch.

Microsoft Surface Percision Mouse Smart Switch

Smart Switch on the Surface Precision Mouse can be enabled by using the Microsoft Mouse and Keyboard Center. It allows you to setup the workplace layout in the app and now you can move the cursor to the border of the screen and the mouse will seamlessly switch to the other device. So if you are working on your desktop and you have your notebook right next to it, you can easily move the mouse from one device to the other.

Overall I am super happy with the new device!

 

Surface Precision Mouse Specs

The mouse also works with Windows 7, Windows 10, Windows 8.1 and macOS devices as long as they support Bluetooth 4.0 or higher.

 
Interface USB 2.1, Bluetooth® Low Energy 4.0/4.1/4.2 Dimensions 4.8 x 3.05 x 1.7 (122.6 mm x 77.6 mm x 43.3 mm)
Wireless Frequency 2.4GHz frequency range Weight 4.76 ounces (135 grams) including rechargeable batteries
Buttons 6 buttons, including right and left click and scroll wheel button Battery Rechargeable lithium ion battery (included)
Design Ergonomic design with side grips Battery Life Up to 3 months
Scrolling Smooth or magnetic detent customizable horizontal and vertical scrolling1 Color Gray