I wanted to be able to unzip files at a command-prompt and use utilities like wget, dig, whois, etc. Of course, if you're working in a command-line environment, you need a zip/unzip utility, so I chose 7-Zip. I downloaded all of the various tools to a directory (oops, I mean folder) and proceeded to set environment variables so I could use them from any directory (oops, I mean folder) on the computer. I did the usual stuff which you're probably familiar with: I went to the System Properties>>Advanced System Settings and clicked the button for Environment Variables. Then I went to the System variables section and modified the path to include the location of the various utilities, especially 7-Zip. I rebooted my system and all of the new paths worked in PowerShell except 7-Zip. I couldn't execute "7z e", for example, to extract the contents of a zipped file. I rechecked everything to make sure I hadn't fat-fingered anything, and everything was fine. Still, I kept getting the error "Bad numeric constant: 7". Huh? I Googled and discovered that Windows PowerShell doesn't like executables that start with a numeric character! Huh! What kind of BS is this??? Oh well, it is what it is and I can't change that, so I decided to create an alias so I could start "7z" with "sz" instead. Makes sense, right? I entered the following command in PowerShell: set-alias sz "c:\program files\7-Zip\7z.exe" and, voila, the alias worked great. Except for one thing; when I shutdown and restarted PowerShell, the alias went away. Grrrrr. Well, I'm new to PowerShell, but there had to be a way to create persistent aliases, right? Well, yes, but it ain't very easy, thanks to Windows security settings. You have to add the alias to your profile. Here's how I got it to work. First of all, I created the profile. Here are the steps:
- Find out where PowerShell thinks your profile is located with the $profile command. Mine was located at C:\Users\don\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1. The WindowsPowerShell directory did not exist, nor did the file Microsoft.PowerShell_profile.ps1, so I had to create both of them.
- Use the command "notepad $profile" to open your profile for editing and enter the alias in the file: set-alias sz "c:\program files\7-Zip\7z.exe"
- Save the file.
- You would think that would do it, but if you exit and restart PowerShell, you'll get a big fat permissions error saying bad things about scripts and configuration files (\Microsoft.PowerShell_profile.ps1 cannot be loaded because the execution of scripts is disabled on this system.). Turns out you have to set signing permissions for the configuration file in order to make this work.
- Close PowerShell and re-open it as Administrator.
- Use the command Get-ExecutionPolicy to see the current execution policy setting. Mine was set to restricted, which is one of the four values available: Restricted, AllSigned, RemoteSigned, or Unrestricted
- I'm not going to go into the security implications of each of the settings, but you really should read up on Set-ExecutionPolicy before messing with these settings. You've been warned. I then used "Set-ExecutionPolicy RemoteSigned" to modify the script/configuration security settings to allow PowerShell to read the new profile.
- I restarted PowerShell and now my alias worked fine.
- What a pain-in-the-neck. I hope this helped you! Let me know if I overlooked anything.