Scripting help from the Windows Admin Center

While it may be tempting to think of the Windows Admin Center as little more than an alternative to the Server Manager, the Windows Admin Center includes some helpful features that often go unnoticed. One such feature is a collection of PowerShell scripts that can be used to automate common tasks. You will have to do a little bit of work in order to use these scripts, but in this article, I will show you what you need to know.

Locating Scripts

As I’m sure you know, the Windows Admin Center is arranged by function. A tree view on the left side of the screen displays categories such as Devices, Events, and Installed Apps. Selecting one of these categories exposes management tasks related to the category. If you look at the figure below for example, you can see that I have selected the Devices tab, which reveals options to disable devices and to update drivers.

The Devices tab exposes options to disable devices or to update drivers.

As you look at the screen capture above, you will notice that there is a PowerShell icon located in the toolbar at the top of the screen. Clicking on this icon does not launch PowerShell, but rather causes the Windows Admin Center to display a collection of scripts that are related to the tab that is selected. In the next figure for example, you can see PowerShell scripts for devices.

The Windows Admin Center contains a collection of PowerShell scripts related to devices.

If you were to scroll through the script that is shown in the figure above, you would find that the script is really long. Additionally, if you were to copy the script to Notepad and save the text as a PS1 file, you would find that your saved script doesn’t do anything. Additionally, there isn’t an option to run the script from within the Windows Admin Center. So what is going on here?

The scripts that Microsoft provides through the Windows Admin Center were never intended to be used as-is. That doesn’t mean that you can’t use them. You just need to understand what it is that Microsoft is really providing.

If you look back at the previous screen capture, you will notice that the script starts with a function declaration (as indicated by the word function). While it is totally normal for a PowerShell script to include one or more functions, the scripts that are included with the Windows Admin Center are nothing but a collection of functions. There are no function calls. The reason why the scripts don’t do anything when you run them is that the scripts are composed solely of functions which are never executed.

So how can you take the code that Microsoft is providing through the Windows Admin Center and use it? If you look at the previous figure, you will notice that there is a Script Name drop down that is located just above the script block. This drop down list, which you can see in the next figure, contains a list of the functions that are included within the script.

You can use the Script Name drop down list to locate a function within a script.

When you select a function from the list, the Windows Admin Center does two things. First, it goes to the location within the script where the selected function begins. Second, the Windows Admin Center selects all of the code that is associated with that particular function (aside from the word function, which is inexplicably left deselected). You can see what this looks like in the next figure.

Choosing a function from the list causes it to be selected.

While having the Windows Admin Center to automatically select the code that is associated with a particular function might not seem like a big deal, some of the functions are really long and it can be tough to figure out where they begin and end. As such, having the Windows Admin Center to automatically select the function’s code can be quite helpful.

Once the function code has been selected, you can paste the code into Notepad and then save it as a PS1 file. Once again though, you aren’t going to be able to use the code as-is. There are two simple, but important things that you will have to do in order to use the function code.

If you take a look at the next figure, you can see that I have pasted a function into Notepad, but I have made two modifications to the code. The first modification was that I added the word Function to the very beginning of the script. Remember, the Windows Admin Center for whatever reason omits the word function from the selected code.

The other thing that I have done was to add a function call to the end of the script. In PowerShell, every function has a name. That name is whatever comes immediately after the word function. In the case of this particular script, the function is named Get-CimWin32LogicalDisk. You can call a function simply by entering the name of the function. As you can see in the figure for example, I have added the words Get-CimWin32LogicalDisk to the end of the script. That’s my function call.

This is what the modified script looks like.

This is what happens when I run the script.

In most cases, you can use the functions that Microsoft has provided with very little work required on your part. In some situations however, you may have to include one or more parameters within your function call. Not all functions require parameters, but there are some that do. When you paste a function into Notepad, look for a section labeled Param. If such a section exists, it will show you exactly what type of parameters the function needs in order to work properly. If a parameter is required, you can append it to your function call.

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top