The humble variable

Powershell has mushroomed recently into almost every facet of systems administration. From Exchange servers to system information management, IT professionals without programming backgrounds are working with scripts more than ever.

The ability to automate almost every aspect of operations is tantalizing. But before we unleash an automated process on critical infrastructure, we should understand what it does.

To that end, let’s look at a fundamental idea: the variable.

Remember when we talked about arrays last month? A series of containers to store your stuff that could be referenced by name or item number?

The variable is even simpler. A variable is a single container that you create. All it has is a name. What is in the container could stay static, or it could change. Think of it like a bucket with a name written on it. The variable name stands for everything in the bucket.

Variables are commonly used in a couple of ways.

The first is to store a value temporarily. This is often present in browser-side scripting like Javascript. If the operations performed on the page don’t really require a big database or even an array, why not just create one bucket to hold that value?

In Javascript, PHP or Powershell, the variable comes into existence as soon as you declare it.

$mybucket = "The text string I'm storing in the variable called mybucket";

In one line, we’ve created a new variable called mybucket and put something inside of it: a string of text. We could just as easily fill it with an integer, a URL or a filename on our web server. From this moment forward, the entire text string can be represented simply by the variable name. Want to change the text in your form field?

document.getElementByID("mytextfield").value = $mybucket;

In Powershell and PHP, declaring a variable also starts with a $ sign.

$rcpath = "c:\users\jimmyjoe\appdata\local\microsoft\outlook\roamcache";

Want to write a script that works with Jimmy Joe in accounting’s Outlook cache for Autocomplete entries, but don’t feel like typing out the entire path every time you want to reference it? Your friend the variable has your back.

So when you’re staring down the unforgiving scramble of an administration script with a bunch of $ signs in it, don’t panic. They’re only buckets, and now you know a little bit more about what they do.

Automating backups with Beyond Compare 4

Beyond Compare 4 by Scooter Software is handy for syncing folders. Many people use this application to sync data from servers to local storage devices. But hold on to your chair, my friends. Their folder sync can be automated through a simple script.

How It Works

load "f:\" "\\\Storage Backup\F"
sync mirror:left->right

The first line states the local source folder and the destination folder on your storage device.

The second line syncs files from the left folder to the right folder — nothing crazy.

This script can be saved in a text file and run as an automated task as often as you like. If you want a log of the results, you can add another line.

log normal "c:\myscripts\synclog-f.txt"

This saves a log with any failed files, the timestamp and if the script completed. Now you can use the time you spent manually syncing folders for more important activities, like reading blogs.

How to remove unwanted Windows 10 apps

Windows 10 is proving stable and useful. But what if you want a cleaner environment? What if you don’t like Bing News? What if you don’t want to Bing anything?

Powershell can help! If you open this command line tool and enter Get-AppxPackage, it returns a list of currently installed apps under your user account.

You can also export the resulting list to a text file with Get-AppxPackage | Out-File c:\users\youareawesome\desktop\yourfilename.txt.

These are not just full-blown programs as seen under Programs & Features, but smaller applications built into Windows. These include both critical pieces like the .NET framework and less useful apps like Bing Food and Drink. The second category are the apps that haunt your Start menu live tiles.

What you’re looking for in the resulting list is the value in PackageFullName for the app you want to weed out. You can even zero in further by using wildcard text in the package call. Get-AppxPackage *bing* returns any apps with “bing” in the name text.

Let’s assume the Bing Health and Fitness app is not going to put more broccoli in the fridge or increase your overall deadlift. Let’s get rid of it.

We copy the PackageFullName of the app. In this case, it is Microsoft.BingHealthAndFitness_3.0.4.336_x64__8wekyb3d8bbwe.

This helps build our overall command in Powershell to remove it:

Get-AppxPackage Microsoft.BingHealthAndFitness_3.0.4.336_x64__8wekyb3d8bbwe | Remove-AppxPackage

If the command runs successfully, no errors show and the cursor displays a second time. Bing Health and Fitness has been removed from your system, but only within the current user account. It still lurks elsewhere.

To remove the app for all users of your machine, our command needs the switch -AllUsers. It goes into our command behind the first cmdlet, like so:

Get-AppxPackage -AllUsers Microsoft.BingNews_4.11.156.0_x86__8wekyb3d8bbwe | Remove-AppxPackage

Running this command for all users requires administrative privileges on your system. If you’re a local administrator and the command is still denied, try running Powershell as administrator.

If you’d like to try it out, you can download our sample Powershell script: Remove Windows 10 Apps. This removes some of the most common third-party apps from Windows 10 for all users. Feel free to modify in the same format to clear out whichever apps you want.

Happy cleaning!