Start-Process Elevated Powershell & Send a Variable to Be Used When Running Elevated Script

When this scirpt is executed(non-elevated) this script asks password and maps onedrive and then it automatically launch powershell(elevated) and asks again password for bitlocker.

If single password is used for onedrive and bitlocker, how to make it to require password once? Or is it possible to pass variable(contains user input password) to new elevated powershell to be used in script to unlock bitlocker?

#
# (FIRST SET EXECUTION POLICY WITH ELEVATED POWERSHELL) Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
#

param([switch]$Elevated)

function Test-Admin {
    $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
    $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}

if ((Test-Admin) -eq $false)  {
    if ($elevated) {
        'tried to elevate, did not work'
    } else {


        ### START - CODE FOR "NON-ELEVATED POWERSHELL"
        
        $pwd = Read-Host 'Enter PW:' -AsSecureString
        
        $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd)
        $UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
        net use O: "" /user: $UnsecurePassword /p:no
        
        ### END - CODE FOR "NON-ELEVATED POWERSHELL"


        # RUN "ELEVATED POWERSHELL"
        Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
    }
    exit
}


### START - CODE FOR "ELEVATED POWERSHELL"

$pwd = Read-Host 'Enter PW:' -AsSecureString

Unlock-BitLocker -MountPoint "D:" -Password $pwd

exit

### END - CODE FOR "ELEVATED POWERSHELL"
3
Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article