Tag: PowerShell

  • Automatically Run Specific PowerShell Scripts at the Start of Every PowerShell Session

    To automatically run specific PowerShell scripts at the start of every PowerShell session, you can modify your PowerShell profile file. PowerShell profiles are scripts that run at the start of a new PowerShell session. Here’s how to add an external script to your PowerShell startup:

    1. Find or Create Your PowerShell Profile: First, you need to determine if you already have a profile and where it is. Open PowerShell and run the following command:

      $profile
      

      This command will show the path to your current user’s profile for the PowerShell console. If you want to add the script for all users or for the ISE, you might need a different profile path.

    2. Check if the Profile Exists: Check if the profile already exists by running:

      Test-Path $profile
      

      If this returns False, the profile does not exist, and you’ll need to create it.

    3. Create the Profile if Necessary: If the profile doesn’t exist, you can create it by running:

      New-Item -path $profile -type file -force
      
    4. Edit Your Profile: Open the profile file in a text editor. You can do this from PowerShell as well, for example, using Notepad:

      notepad $profile
      
    5. Add Your Script to the Profile: In the profile file, you can add commands to run at startup. To run an external script, use the following command:

      . C:\path\to\your\script.ps1
      

      Replace C:\path\to\your\script.ps1 with the actual path to your script. The dot sourcing operator (.) before the path ensures that the script runs in the current scope, so any functions or variables it defines will be available in your session.

    6. Save and Close the Profile: After adding your script, save the profile file and close the text editor.

    7. Test Your Profile: Open a new PowerShell window to test if your script runs as expected. If there are any errors in the script, they will show up when you start PowerShell.

    Keep in mind that running scripts from an external source can pose a security risk. Make sure you trust the scripts you’re adding to your PowerShell startup. Additionally, if your system’s execution policy is set to restrict script execution, you may need to adjust it to allow your profile and scripts to run. You can check the current execution policy by running Get-ExecutionPolicy, and set a new policy with Set-ExecutionPolicy, though changing execution policies should be done with understanding the security implications.null

  • Error The update is not applicable to your computer When Installing PowerShell V3

    PowerShell V3 and PowerShell ISE V3 is not a port of Windows 7 and Windows Server 2008 R2. However, you can download it from here.

    You might receive following error when you install the msu file

    The update is not applicable to your computer

    Solution

    1. Installed Windows 7 and Windows Server 2008 R2 Service Pack 1
    2. Install .NET 4.0 Full Profile. ( not client profile )
  • Install PowerShell and Powershell ISE on Windows Server

    Windows Server 2003

    For Windows server 2003, PowerShell V1 was released as KB ( hotfix). Download and install KB926139 – Windows PowerShell 1.0 English-Language Installation Package for Windows Server 2003

    If you need PowerShell V2, make sure you have SP2 installed and then install Windows Management Framework from http://support.microsoft.com/kb/968929

    Windows Server 2008

    For Windows Server 2008 run the following command in an elevated Command Prompt.

    servermanagercmd -install PowerShell

    Windows Server 2008 R2

    Windows Server 2008 R2 has PowerShell V2 built in and enabled by default, however, PowerShell ISE is not. It is installed on Windows 7 client OS, but an Optional Component on Windows Server 2008 R2. To Install PowerShell ISE on R2, run the following command in a Windows PowerShell Modules prompt

    Import-Module ServerManager
    Add-WindowsFeature PowerShell-ISE

    OR

    1. In Server Manager, start the Add Roles and Features wizard.
    2. On the Features page, select Windows PowerShell ISE.

    Windows Server 2012

    Windows Server 2012 has both PowerShell V3 built in and installed. PowerShell ISE needs to be added as it’s an optional Windows PowerShell feature, same as Windows Server 2008 R2.

    Also checkout How to run PowerShell scripts to start your first PowerShell script.

  • Using SCVMM 2012 Cmdlets in PowerShell

    SCVMM Cmdlets allow SCVMM admin/users to do everything they can do in SCVMM AdminConsole in windows PowerShell command line. In SCVMM 2008 R2, you can run following command in PowerShell to run SCVMM cmdlets.

    Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager

    But this does not work in SCVMM 2012 as SCVMM 2012 uses PowerShell module. For SCVMM 2012 now you need run following in PowerShell instead

    Import-Module "C:Program FilesMicrosoft System Center 2012Virtual Machine ManagerbinpsModulesvirtualmachinemanagervirtualmachinemanager"

    (This assumes that you have SCVMM 2012 installed on the default location C:Program FilesMicrosoft System Center 2012 )

    The PowerShell cmdlets in SCVMM 2012 change a lot in SCVMM 2012, you can get a list of all SCVMM 2012 cmdlets by typing the following at the PowerShell command shell prompt

    Get-Command -Module virtualmachinemanager -Type cmdlet

    And you can run Get-Help on any of the cmdlets to get the syntax for that cmdlet.

    Get-Help <cmdlet name> -detailed

    Update: Microsoft has published the SCVMM 2012 cmdlets help to TechNet, check it out http://technet.microsoft.com/en-us/library/hh801697.aspx

  • Configure PowerShell to use DotNet 4.0

    You might get following error when you load a snap-in that is written by DotNet 4.0.

    This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

    This is because by default, PowerShell uses DotNet version 2.0 CLR. To use powershell load DotNet 4.0 assemblies, the following settings need to be added in PowerShell.exe.config under C:WindowsSysWOW64WindowsPowerShellv1.0.

    <configuration>
        <startup useLegacyV2RuntimeActivationPolicy="true">
            <supportedRuntime version="v4.0.30319"/>
            <supportedRuntime version="v2.0.50727"/>
        </startup>
    </configuration>

    Note:

    1. Above applies to 64bit windows server 2008 r2. On a 32 bit machine, PowerShell.exe.config can be found at C:windowsSystem32WindowsPowerShellv1.0
    2. Create PowerShell.exe.config if this file is not found.
  • How to run ps1 scripts

    PowerShell scripts are text files with ".ps1" extensions that contain PowerShell commands. Starting with Windows Server 2008, PowerShell is built into the OS. For earlier OS versions, you will need to download PowerShell at Microsoft PowerShell Site and install it.

    To run ps1 scripts, you first need to enable scripts by running:

    Set-ExecutionPolicy RemoteSigned

    Otherwise, you will get this error message:

    File <script> cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.

    The most common way to run PowerShell scripts is from Windows PowerShell command prompt. You must specify a path to the script to run it. If the script is in your current folder, then you can run it with:

    .script.ps1

    If the script is in another folder, you need specify the full path:

    c:scriptsscript.ps1

      (more…)

    1. Install PowerShell From Command Line

      To install PowerShell on Windows Server 2008 R2 or Hyper-V Server 2008 R2, following features needs to be installed.

      1. NetFx2-ServerCore
      2. NetFx3-ServerCore
      3. MicrosoftWindowsPowerShell

      Because Powershell is built on DotNet framework, DotNet 2.0 and 3.5  has to be installed prior to PowerShell installation.

      To install PowerShell from command line, run following from elevated command prompt

      DISM.exe /online /enable-feature /featurename:NetFx2-ServerCore DISM.exe /online /enable-feature /featurename:NetFx3-ServerCore DISM.exe /online /enable-feature /featurename:MicrosoftWindowsPowerShell

      Or run following command if it’s an 64bit machine

      DISM.exe /online /enable-feature /featurename:NetFx2-ServerCore-WOW64 
      DISM.exe /online /enable-feature /featurename:NetFx3-ServerCore-WOW64 
      DISM.exe /online /enable-feature /featurename:MicrosoftWindowsPowerShell-WOW64