Category: Power Shell

  • Connecting PowerShell to Exchange Online

    This article describes the procedure of connecting PowerShell to Office 365 in order to manage the Cloud via CMDLET

    Prerequisites:

    1. PowerShell version 7.2 or higher
    2. A valid Global Administrator account in Office 365 Exchange Online

    Procedure:

    • Allow execution Policy to remotesigned
    Set-ExecutionPolicy RemoteSigned
    • Set minimum TLS Protocol to TLS 1.2
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    • Install necessary Modules for PowerShell: AzureAD, MSOnline, Exchange Online Management
    Install-Module AzureAD
    Install-Module MSOnline
    Install-Module -Name Exchangeonlinemanagement
    • Import the installed modules
    Import-Module AzureAD
    Import-Module MSOnline
    Import-Module -Name Exchangeonlinemanagement
    • Connect to ExchangeOnline
    Connect-ExchangeOnline

    Note: A browser login windows will pop-up. Use your global admin credentials and close the pop-up window. You are logged in now:

  • Adding WiFi QR-Code

    Install PowerShell Cmdlets

    Install-Module -Name QRCodeGenerator -Scope CurrentUser -Force

    Configure your WiFi Information

    New-QRCodeWifiAccess -SSID 'Your WiFi Sid here' -Password 'Your WiFi PASS here' -Show
  • Power Shell 7 Installation & Setup:

    1.Adding Code Repository Listing in Order to copy code to clipboard from PowerShell Directly

    Invoke-RestMethod -Uri https://tinyurl.com/codeAusBuch -UseBasicParsing | New-Item -Path function: -Name L -Force | Out-Null

    2. Installing PowerShell

    Invoke-RestMethod -Uri https://aka.ms/install-powershell.ps1 -UseBasicParsing | New-Item -Path function: -Name Install-PowerShell | Out-Null

    or

    Install-PowerShell -Destination c:\ps7test -AddToPath

    or

    Install-PowerShell -UseMSI

    3. Checking which PowerShell has been started

    [Environment]::CommandLine

    4. Path to started PS

    (Get-Process -Id $pid).Path

    5. Install Chocolatey (Listing 1.5)

    Invoke-RestMethod -UseBasicParsing -Uri 'https://chocolatey.org/install.ps1' | Invoke-Expression

    6. Install Scoop (Listing 1.6)

    Invoke-RestMethod -UseBasicParsing -Uri 'https://get.scoop.sh' | Invoke-Expression
    scoop install git
    scoop bucket add extras

    7. Allow installing Scripts in PowerShell

    Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force

    11. View Execution Policy

    Get-ExecutionPolicy -List

    8. Console Grid Installation (For machines with no GUI – such us WinServer Core)

    Install-Module -Name Microsoft.Powershell.ConsoleGuiTools -Scope CurrentUser

    9. Process Listing (Listing 1.7)

    Get-Process |
    >> Where-Object MainWindowHandle |
    >> Select-Object -Property Name, Id, Description |
    >> Sort-Object -Property Name |
    >> Out-ConsoleGridView -Title 'Prozesse' -OutputMode Multiple |
    >> Stop-Process -WhatIf # Delete WhatIf to end simulation

    10. Extending PowerShell Help

    Update-Help -UICulture EN-US -Force -Scope CurrentUser #delet Scope CurrentUser - for Windows Power Shell

    Useful Scripts

    1. Script for Changing Power Shell appearance – display warnings blue, – powershall path at the top, -adding Listing as standard to powershell (Listing 1.15)

    $host.PrivateData.ErrorForegroundColor = 'Blue'
    Invoke-RestMethod -Uri https://tinyurl.com/codeAusBuch | 
    New-Item -Path function: -Name L -Force | 
    Out-Null

    2. Script for duplicating profiles for both WinPS and PS. Once executed Copy the above script (listing 1.15) in the opened notepad and save to default location

    – can be executed with: L 1.16 -run

    #requires -RunAsAdministrator
    # Hardlinks können nur als Administrator angelegt werden
    
    # dieses Skript macht nur Sinn, wenn das Betriebssystem "Windows" ist:
    if ($isWindows -eq $false) 
    {
        Write-Warning "Spiegeln der Profile ist nur bei Windows möglich."
        Write-Warning "Andere Betriebssysteme verfügen über kein Windows PowerShell"
        return
    }
    
    # sicherstellen, dass die Windows PowerShell Profildatei existiert
    if ($PSVersionTable.PSEdition -eq 'Core')
    {
        # Pfad zum Windows PowerShell Profilskript berechnen: 
        $winprofile = $profile.CurrentUserAllHosts -replace '\\PowerShell\\', '\WindowsPowerShell\'
        $coreprofile = $profile
    }
    else
    {
        # Pfad steht bei Windows PowerShell schon in $profile:
        $winprofile = $profile.CurrentUserAllHosts
        $coreprofile =  $profile -replace '\\WindowsPowerShell\\','\PowerShell\'
    }
    
    # Datei anlegen, wenn noch nicht vorhanden
    $exists = Test-Path -Path $winprofile
    if ($exists -eq $false)
    {
        $null = New-Item -Path $winprofile -ItemType File -Force
        Write-Warning "Profilskript für Windows PowerShell angelegt."
    }
    else
    {
        Write-Warning "Profilskript für Windows PowerShell war vorhanden."
    }
    
    # testen, ob Profilskript für PowerShell existiert
    $exists = Test-Path -Path $coreprofile
    if ($exists)
    {
        # ist die Profildatei vielleicht bereits gespiegelt?
        $datei = Get-Item -Path $coreprofile
        if ($datei.LinkType -eq 'HardLink')
        {
            # wir sind fertig
            Write-Warning 'Profildatei von Windows PowerShell wird bereits mitgenutzt (keine Änderungen).'
    
            # Profilskript im Editor öffnen:
            Invoke-Item -Path $winprofile 
            Return
        }
        else
        {
            # wenn PowerShell bereits eine eigene Profildatei verwendet, muss diese
            # gelöscht werden, um stattdessen auf das Windows Profil zu verweisen
            # also nachfragen, ob der User das will:
            Write-Warning 'Profilskript für PowerShell ist bereits als separate Datei vorhanden.'
            Write-Warning 'Soll dieses Profil gelöscht und stattdessen das Profil der Windows PowerShell verwendet werden?'
    
            # Frage stellen und 20 Sekunden warten, danach "NEIN" wählen:
            choice.exe /C JN /M "Drücken Sie J für Ja oder N für Nein" /T 20 /D N
            
            # choice.exe liefert einen ErrorLevel in $LastExitCode zurück, der
            # Zahlenwert entspricht der Auswahl (1=Ja, 2=Nein)
            if ($LASTEXITCODE -eq 2) { return }
            
            # Profildatei löschen
            Remove-Item -Path $coreprofile -Force
        }
    }
    
    # Hardlink anlegen (erfordert Adminrechte)
    $null = New-Item -Path $coreprofile -ItemType HardLink -Value $winprofile 
    
    Write-Warning 'Profildatei von Windows PowerShell wird nun mitgenutzt.'
    Write-Warning "Um die Spiegelung aufzuheben, einfach die $profile löschen und von Hand oder im Editor neu angelegen."
    
    # Profilskript im Editor öffnen:
    Invoke-Item -Path $winprofile

    GIT HUB Desktop

    this is a good tool to download all or divers spripts all at once:

    https://desktop.github.com/

    Clone the Repository with the following address

    https://github.com/TobiasPSP/OReilly

    Power Shell updating

    1. Check Power Shell Version

    $PSVersionTable.PSVersion

    2. Check PowerShellGet version

    Get-Module -Name PowerShellGet -ListAvailable

    3. Conduct update on both PowerShellGet & PowerShell

    Install-Module -Name PowerShellGet -Scope CurrentUser -Force -AllowClobber
    Install-Module -Name Packagemanagement -Scope CurrentUser -Force -AllowClobber