Getting ready for PowerShell .NET Notebook

The latest release of .NET Interactive Preview 2 (February 6), which includes .NET Notebook for PowerShell. Remember, this is a .NET Core component that is available cross-platform.

This is great! You can start using notebook file and share it across many systems, both Windows and Linux Operating Systems.

Check out Microsoft blog post on “Public Preview of PowerShell Support in Jupyter Notebooks.”

Before you continue, I suggest to get Anaconda 2019.10 (v4.8.1) installed in your system.

Installing .NET Interactive in Ubuntu

In Windows, just takes a few steps to set it up. For Linux, it takes a few extra steps but still is quick enough to get you started.

For Windows, follow the instructions found at the .NET Interactive page in Github.

For Linux, for Ubuntu 18.04, follow the blog post “Ubuntu 18.04 Package Manager – Install .NET Core“.

Basically, in either operating systems, you install:

  • Install the .NET Core SDK
  • Install the ASP.NET Core runtime
  • Install the .NET Core runtime

After these components are installed, proceed to install .NET Interactive Tools, which will include PowerShell support in Jupyter Notebook.

1. Install the .NET Interactive Global tools with this simple command:

$ dotnet tool install --global Microsoft.dotnet-interactive

2. Then install .NET Interactive “Jupyter” component with the following command:

$ dotnet interactive jupyter install

At this point, in Ubuntu, you will encounter the following known error: (see image)

To resolve the issue, use the text editor to open the ~/.bashrc file to add the path to .NET Tools folder:

$ sudo vim ~/.bashrc
## - Add path to .NET Tools:
export PATH=$PATH:~/.dotnet/tools
:wq
$ source ~/.bashrc

Now, we rerun the command, and this time it will complete without any errors:

$ dotnet interactive jupyter install

To verify that all Jupyter kernel was installed, execute the following command:

$ jupyter kernelspec list

Now, you’re ready to work with PowerShell Jupyter Notebook.

Starting Jupyter Notebook

In Windows, you use any console application to start a Jupyter Notebook session using: DOS, Windows PowerShell, and even PowerShell 7 Preview. Have you to use the Anaconda menu shortcut has provided for running the Windows PowerShell prompt?

Better yet, check my instructions on how to create the “Anaconda Pwsh7 Preview Prompt” shortcut in my previous blog post “ANACONDA AND POWERSHELL WORKING TOGETHER!“.()

Back in Linux, open a bash terminal session.

Now, to start a .NET Interactive Jupyter Notebook session, at the console prompt type the following command:

jupyter lab

At this point, the Jupyter Notebook will open on your default browser (Windows or Linux).

The launcher will show all available components for creating notebook files.

Just pick the notebook kernel you wish to start working… let say “.NET PowerShell.”

Notice that I running the $PSVersionTable in the Notebook that the .NET PowerShell kernel is one release behind the latest update.

Now that I test that my .NET Notebook works, I can save my results for later use.

Please, if you encounter any issues with .NET Interactive/.NET Notebook, post them in their Github repo.

Wait! How can I get PowerShell 7 Preview RC 2 updated in .NET Interactive?

I did post the issue about why I was getting PowerShell 7 Preview RC 1 instead of RC2 and got the answer.

It looks like the initial build of .NET Interactive installation will install version ‘1.0.110801‘, which includes PowerShell 7 Preview RC1.

To get the latest build available with PowerShell 7 Preview RC 2, you need to run the update command:

## - To update tool - use PowerShell 7 Preview RC2
dotnet tool update -g --add-source "https://dotnet.myget.org/F/dotnet-try/api/v3/index.json" Microsoft.dotnet-interactive

Run the “jupyter lab” command again and run again the saved *.ipynb.

And that’s it!  As you can see, this command can get your .NET Interactive installation refreshed with the latest build.

Some exciting features are coming down the pipeline. Stay tuned for more!

Anaconda and PowerShell working together!

Yes! To my surprise, when I completed installing the latest update of Anaconda (Anaconda3 2019.10 (64bit) v4.8.1), I realized they have included the following menu item: “Anaconda PowerShell Prompt (Anaconda3)“. Apparently, this menu item has been added for some time.

So, we can take advantage of this shortcut, especially when we can use this console prompt for working with “PowerShell Notebook.” Please, check out Rob Sewell blog post on the recent update .NET Notebook Preview 2 post about “New .NET Notebooks are here – PowerShell 7 notebooks are here.“.

But, Wait! Let’s take this a little further and get you ready to do some fun.

What’s the main advantage?

The “Anaconda PowerShell Prompt” shortcut is already set to activate Anaconda to be used with Windows PowerShell. There’s no need to do a manual activation by opening a DOS command shell and executing:

c:\> conda activate

Trying to use Python without activating Anaconda, it will give you a message.

The activation will allow you to use Python within Windows PowerShell. Or, just use the shortcut “Anaconda PowerShell Prompt.”

As you probably will notice, this menu item only open Windows PowerShell. So, what about PowerShell Core?

This is probably because of PowerShell Core has multiple versions: PowerShell 6.2.4 (GA) and PowerShell 7 Preview (RC2), both supported by Microsoft.

Would you like to create the Anaconda Pwsh7 Prompt shortcut?

Yes! We can create our own PowerShell Core shortcut. And, here’s how to create the shortcut for Anaconda PowerShell 7 Preview.

First, I will make another copy of the original shortcut and label it “Anaconda Pwsh7-Preview Prompt (Anaconda3)“.

Here’s the original path use the Windows PowerShell shortcut:

%windir%\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'C:\ProgramData\Anaconda3\shell\condabin\conda-hook.ps1' ; conda activate 'C:\ProgramData\Anaconda3' "

And, here’s my shortcut modification to use PowerShell 7 Preview:

%ProgramFiles%\PowerShell\7-preview\pwsh.exe -ExecutionPolicy ByPass -NoExit -Command "& 'C:\ProgramData\Anaconda3\shell\condabin\conda-hook.ps1' ; conda activate 'C:\ProgramData\Anaconda3' "

Keep in mind, you will need administrator privileges to create this shortcut in the ProgramData Anaconda menu.

After making all the necessary changes to the new shortcut, we got both Window PowerShell and PowerShell 7 Preview working with Anaconda.

Now go ahead and expand your scripting knowledge!