$ErrorActionPreference = "SilentlyContinue"
$rand = New-Object System.Random
#--------------------[ Create and relocate to C:\scripts ]----------------------
if (!(Test-Path -pathtype Container "C:\Scripts")){
new-item C:\Scripts -itemType Directory -Force #--[ Create local scripts folder.
}
$TargetPath = "C:\Scripts" #--[ Where the script "should" live
$ScriptFullName = ($MyInvocation.MyCommand).Name #--[ EX: script.ps1
$ScriptFullPath = ($MyInvocation.MyCommand).Path #--[ EX: C:\Scripts\script.ps1
$ScriptHomeDir = split-path -parent $ScriptFullPath #--[ EX: C:\Scripts, where the script resides
$ScriptWorkingDir = $pwd.path #--[ EX: C:\Scripts or C:\temp, where the script executes from
$ScriptShortName = [system.io.path]::GetFilenameWithoutExtension($ScriptFullPath) #--[ EX: script (no extention)
$ErrorDetected = $False
$osv = [environment]::osversion.VersionString #--[ Get Windows version, not required, just for convenience
$windir = [System.Environment]::ExpandEnvironmentVariables("%WINDIR%") #--[ Get %windir% environment variable
#$windir = $env:windir #--[ Alternate format
$ThisComputer1 = [System.Net.Dns]::GetHostName() #--[ NOTE: No reason for both forms, just because.
$ThisComputer2 = $env:COMPUTERNAME
$eMailDomain = "gmail.com" #--[ Domain where emails will be sent
$ErrorDetected = $false
#--------------------[ Assure things run from C:\scripts ]----------------------
if (!($ScriptHomeDir -eq $TargetPath)){ #--[ Paths are NOT same ]--
if (!(Test-Path -path $TargetPath)){ #--[ Does target path exist? If not, create it ]--
New-Item $TargetPath -type directory
}
if (!(Test-Path "$TargetPath\ThisScriptName")){ #--[ Is the script there? If not, copy it there ]--
Copy-Item $ScriptFullPath $TargetPath\$ScriptFullName
}
$WshShell = New-Object -comObject WScript.Shell #--[ Place a shortcut on the desktop of current user to call the script
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\MyScript.lnk")
$Shortcut.TargetPath = 'powershell.exe'
$Shortcut.Arguments = "-WindowStyle Hidden –Noninteractive -NoLogo -Command `"$TargetPath\$ScriptFullName`""
$Shortcut.Arguments =
$Shortcut.Save()
#iex (.{Join-Path $TargetPath \$ScriptFullName})
#--[ re-invoke the script from the new location ]--
#EXIT
#--[ Force termination ]--
}Else{
#--[ Paths ARE good, script exists, OK to execute ]--
#if (!(Test-Path "$ScriptPath\ThisScriptName"))
#--[ an optional additional check ]--
}