Fix "Unable to Find Package" NuGet Error in Visual Studio C#
PowerShell is Microsoft developed a powerful tool for task automation and configuration management consisting of an excellent command-line shell as well as a scripting language. It is designed for Sys Admin and IT professionals for automating a wide range of time-consuming administrative tasks. Powershell is used by developers to test build and deploy solutions in CI/CD environments.
Powershell is initially developed as a scripting language only for Windows and was later available for other OS as well. Initially, it was built on the .Net Framework which includes Powershell 1.0, 2.0,3.0, 4.0,5.0, and 5.1. Later it was built on the .Net Core Framework on 18 August 2016 and due to the cross-platform nature of the .net core PowerShell is also cross-platform and open source and released with the new name PowerShell Core.
So now you can run Powershell in Windows, several latest distributions of Linux and MAC.
Powershell scripts are a series of PowerShell commands written in a text file with a .ps1 extension and these scripts are executed sequentially when a call to the script is made. Like other .exe files, .bat files, and image files you cannot run PowerShell script files by double-clicking it. To run Powershell scripts you need to follow the following steps.
Powershell comes preinstalled on your Windows 7 machine and beyond. If you have any machine before Windows 7 like Windows XP or Windows Vista then you have to separately install them into your machine. But Microsoft always recommends you upgrade your OS and then install a separate Powershell.
The Powershell version preinstalled with your OS directly depends upon the OS version as shown below (source)
PowerShell Version | Preinstalled with | Released For |
1.0 | They are made for Windows XP SP2, Windows Server 2003 SP1, and Windows Vista and have to be separately installed. | Windows XP SP2 Windows Server 2003 SP1 Windows Vista |
2.0 | Windows 7 Windows Server 2008 R2 |
Windows XP SP3 Windows Server 2003 Service Pack 2 Windows Vista SP1 |
3.0 | Windows 8 Windows Server 2012 |
Windows 7 SP1 Windows Server 2008 Service Pack 1 Windows Server 2008 R2 Service Pack 1 |
4.0 | Windows 8.1 Windows Server 2012 R2 |
Windows 7 Service Pack 1 Windows Server 2008 R2 Service Pack 1 Windows Server 2012 |
5.1 | Windows 10 Anniversary Update Windows Server 2016 |
Windows 7 Windows Server 2008 Windows Server 2008 R2 Windows Server 2012 Windows Server 2012 R2 (codename: Windows Server 8.1) |
Core 6.1 | Windows, Mac, and Linux | |
7 | Windows, Mac, and Linux |
Ans: Go to run and type PowerShell and press enter.There type : $PSVersionTable.PSVersion and press Enter. So your Powershell Version is a combination of Major and Minor.
As shown here. Powershell the version installed is Major.Minor ie. 5.1
Only members of the administrative group can change the execution policy of Powershell. Actually, there is 4 execution policy type to execute the script.
a) Restricted: This is the default execution policy for PowerShell. By default, Powershell has Restricted Powershell and no script either local, remote
or download can be run on the system
b) AllSigned: For this, all the scripts are required to be digitally signed by a trusted publisher.
c) RemoteSigned: All scripts which are downloaded from the internet need to be digitally signed by a trusted
publisher to work. All other local scripts are allowed to run.
d) Unrestricted: If the execution policy is unrestricted then no signature is required to run the script.
You can control the level of trust for executing scripts that run on your system.
Now, let's check the current execution policy type you have in your system, the command for this is as follow:
You can change the current execution policy with the Set-ExecutionPolicy cmdlet to any of the above 4 types.
First of all open Powershell with administrative privileges and type the below command.
Replace the policy name with any of the above 4 policy types.
Here, I have changed to RemoteSigned Type, now locally all scripts can be executed, and downloaded scripts required to be digitally signed by a trusted publisher.
FYI if you have not run with admin privilege you will get the below error.
If you want to set it back to default then you can set it with Restricted type because it is the default settings provided by Powershell.
If you have already created scripts then skip these steps.
Let's create scripts using Notepad
For creating a script you just need a text editor like notepad which default comes with Windows.
Open Notepad and write a below simple PowerShell script
Then save it with ps1 extension as shown below. Here, I have given HelloWorld as a name.
You can use inbuilt Powershell ISE for creating the PowerShell scripts and the steps are as follows:
1 Press the Windows + R keyboard shortcut to open the run dialog box. There type powershell_ise.exe and press Enter. This will open Powershell ISE.
2 Click on File > New to create a new PowerShell script. Then type your script there.
3 Then press Ctrl + S keyboard shortcut to save the file and give it a name to the script. In my case I have named it for example: helloworld.ps1 and then click on the Save button.
For running the script you can type the below script:
replace file_location_path with the required file path. Here I have helloworld.ps1 inside E:\ so I will type as E:\helloworld.ps1 as shown below.
as soon as you press Enter you will get the desired output of the script file. This script just displays the message present in the file but you can do lots of automation things with Powershell which is beyond the scope of this tutorial.
If your script file is on the current working directory then you just have to type: .\helloworld.ps1 inside of full file path. Here, PowerShell and script file is inside E:\ so I can just type like below.
You can run the PowerShell script even without changing the policy type. You can even run your current policy is restricted. For this, you have to use a bypass switch as shown below.
If you are having a restricted policy then you are not able to run any scripts (local or remote). Just try it.
File E:\helloworld.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies ...
But there is a safe way, you can load the script without changing any policy with the below script
We can take the help of the Invocation(&) operator to run the PowerShell script if it has space in the path or in the script name.
Syntax Goes like this:
Suppose I have hello world.ps1 file name inside E:\Powershell Tricks Collection folder then I can execute as shown below.
We can run the PowerShell script from the command prompt with the following command:
example:
If you have not enabled Powershell script execution on your System you have to add the parameter as : -ExecutionPolicy Bypass
Powershell ISE stands for Powershell Integrated Script Environment. It is the host application for Windows Powershell where we can write, test and debug scripts and has IntelliSense support which helps while writing scripts. It is supported in all versions of Powershell up to Powershell V5.1 and later for Powershell 6 Core, it is removed.
Now you can use ISE mode in VS Code via these extensions.
How to open Powershell ISE?
Goto run and type "Powershell ISE" then select "Powershell ISE" from the list. Then you will get Powershell ISE as shown below.
The cmdlet is a lightweight special PowerShell command which is used to perform single particular functions like rename items, change current directories, get help about a particular cmdlet, terminate the process, start PC, etc. It can even be used in command prompt. Some of the cmdlets are
Ans: Windows Environment comes preinstalled with Powershell and if you want to install Powershell in Ubuntu Environment then follow below steps:
Here, I am showing installing and using Powershell 6 in Ubuntu 16.4.
# Import the public repository GPG keys
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
# Register the Microsoft Ubuntu repository
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee
/etc/apt/sources.list.d/microsoft.list
# Update apt-get
sudo apt-get update
# Install PowerShell
sudo apt-get install -y powershell
# Start PowerShell
powershell
After registering the Microsoft repository the first time as a superuser, all that is required to keep it up to date after that is to use the command sudo apt-get upgrade Powershell. Then you need to simply run Powershell.