How to set the ExecutionPolicy using Powershell

To set the ExecutionPolicy using Powershell, run Powershell as Administrator.
Type in Set-ExecutionPolicy followed by the ExecutionPolicy you wish to use.

  • Restricted: Restricted is the default execution policy. PowerShell scripts are not allowed to run.
  • All Signed: All Signed means thath if they are signed by a trusted publisher.
  • Remote Signed: Remote Signed means, any PowerShell scripts that have been locally created will be allowed to run.
  • Unrestricted: As the name implies, Unrestricted removes all restrictions from the execution policy.
  • Microsoft has disabled scripting by default to prevent malicious code from being executed.

    Please rate this snippet

    NuGet: Failed to initialize the PowerShell Host

    Today i recieved the following Nuget Error while trying to install Automapper.

    Failed to initialize the PowerShell host. If your PowerShell execution policy setting is set to AllSigned, open the Package Manager Console to initialize the host first.

    To fix that, close all Visual Studio instances and open the Powershell Console as Administrator. Enter Set-ExecutionPolicy RemoteSigned or Set-ExecutionPolicy Unrestricted. Be aware that you need to set the execution policy on both the x86 and the x64 Powershell Console.

    A more simple solution would be to put the code below in a File with an .reg extension and execute that.

    Windows Registry Editor Version 5.00
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell]
    "ExecutionPolicy"="Unrestricted"
    
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell]
    "ExecutionPolicy"="Unrestricted"
    

    Now start Visual Studio and Try Again, if the error appears again, try running Visual Studio as Administrator.

    Please rate this snippet

    How to download a file in Powershell

    To download a file in Powershell you can use the following snippet.

    Sample Powershell

    (New-Object System.Net.WebClient).DownloadFile('http://download.tuxfamily.org/notepadplus/6.6.6/npp.6.6.6.Installer.exe', 'C:\Users\MF\Codesnippets\Desktop\Download\npp.6.6.6.Installer.exe')
    

    Please rate this snippet

    How to delete a file or folder in Powershell

    To delete a file or Folder in Powershell you can use the following snippet.

    Sample Powershell

    if you want to delete a file use

    remove-item -path "C:\Users\Codesnippets\Desktop\Test.xml" -force -recurse
    

    if you want to delete a folder use

    remove-item -path "C:\Users\Codesnippets\Desktop\Folder1" -force -recurse
    

    for more informations see Using the Remove-Item Cmdlet

    Please rate this snippet

    How to start a process and wait for exit in Powershell

    To start a application and wait for exit in Powershell you can use the following snippet.

    Sample Powershell

    start-process -filepath "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe" -Wait -NoNewWindow -argumentlist '-args'
    

    for more informations take a look at the MSDN Start-Process

    Please rate this snippet

    How to check if file or folder exists in Powershell

    To check if file or folder exists in Powershell you can use the following snippet.

    Sample Powershell

    for files use it like this

    if(test-path "C:\Users\Codesnippets\Desktop\Test.xml") 
    { 
    	echo "exists!" 
    } 
    else 
    { 
    	echo "not exists!" 
    }
    

    for folders/directories use it like this

    if(test-path "C:\Users\Codesnippets\Desktop") 
    { 
    	echo "exists!" 
    } 
    else 
    { 
    	echo "not exists!" 
    }
    

    Please rate this snippet