Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
quonic authored Feb 13, 2021
1 parent d6c8519 commit d68f749
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
27 changes: 27 additions & 0 deletions DownloadPowerShell.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@echo off
rem Create PowerShell Folder
if exist .\PowerShell\ (
echo PowerShell Folder Already exists
) else (
echo Creating PowerShell Folder
mkdir .\PowerShell
)
rem Download PowerShell from https://github.com/PowerShell/PowerShell/releases/latest
if exist .\PowerShell.zip (
echo PowerShell.zip found, skipping download
) else (
echo PowerShell.zip not found, downloading PowerShell.zip
rem Get the bit type of the processor
reg Query "HKLM\Hardware\Description\System\CentralProcessor\0" | find /i "x86" > NUL && set OS=32BIT || set OS=64BIT
rem Download the 32bit version or 64bit version of PowerShell
if %OS%==32BIT c:\Windows\System32\windowspowershell\v1.0\powershell.exe -Command "& { $uri = $((Invoke-WebRequest -UseBasicParsing -Uri 'https://github.com/PowerShell/PowerShell/releases/latest').Links.href | Where-Object { $_ -like '*releases*win-x86.zip' }); Invoke-WebRequest -Uri $('https://github.com'+$uri) -OutFile '.\PowerShell.zip' }"
if %OS%==64BIT c:\Windows\System32\windowspowershell\v1.0\powershell.exe -Command "& { $uri = $((Invoke-WebRequest -UseBasicParsing -Uri 'https://github.com/PowerShell/PowerShell/releases/latest').Links.href | Where-Object { $_ -like '*releases*win-x64.zip' }); Invoke-WebRequest -Uri $('https://github.com'+$uri) -OutFile '.\PowerShell.zip' }"
)
rem Extract the Downloaded Powershell Zip
if exist .\PowerShell\pwsh.exe (
echo PowerShell already extracted
) else (
echo Extracting contents of PowerShell.zip to .\PowerShell\
c:\Windows\System32\windowspowershell\v1.0\powershell.exe -Command "& { Add-Type -AssemblyName System.IO.Compression.FileSystem; [System.IO.Compression.ZipFile]::ExtractToDirectory('.\PowerShell.zip', '.\PowerShell') }"
echo Done extracting PowerShell.zip to .\PowerShell\
)
2 changes: 2 additions & 0 deletions Example.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Requires -Version 7
Write-Output $PSVersionTable
3 changes: 3 additions & 0 deletions RunExample.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off
echo ==== Running Example.ps1 With pwsh.exe found under .\PowerShell\ ====
.\PowerShell\pwsh.exe -WorkingDirectory .\ -NoExit -NoProfile -NoLogo -File .\Example.ps1
25 changes: 25 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# PowerShell Standalone Example

## Requirements

* PowerShell 2.0 or greater
* Server 2008 or greater
* A little bit of Batch and PowerShell knowledge to get a PowerShell script to run

## Run a PowerShell script with PowerShell 7 with out installing PowerShell 7

1. Copy the files in this repository into a folder
2. Navigate to that folder
3. Double click on `DownloadPowerShell.bat`
4. Wait for PowerShell to download and extract into the `PowerShell\` folder
5. Double click on `RunExample.bat`
6. You should see the output of `$PSVersionTable`

## How to use in production

1. Do the steps above from 1 to 4
2. Edit and Rename `RunExample.bat` and `Example.ps1` as needed
3. Delete `PowerShell.zip` and `DownloadPowerShell.bat`
4. Zip your `bat`, `ps1`, and the `PowerShell` files and folders
5. Move you zip file to production
6. Extract and run you `bat` file

0 comments on commit d68f749

Please sign in to comment.