Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8977103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:17:46+00:00 2026-06-15T19:17:46+00:00

When storing text temporarily in powershell variables at runtime, what is the most efficient

  • 0

When storing text temporarily in powershell variables at runtime, what is the most efficient way of removing a variables contents from memory when no longer needed?

I’ve used both Clear-Item variable: and Remove-Variable but how quickly does something get removed from memory with the latter vs nulling the memory contents with the former?

EDIT: I should have made it a little clearer why I am asking.

I am automating RDP login for a bunch of application VMs (application doesn’t run as a service, outsourced developers, long story).

So, I am developing (largely finished) a script to group launch sessions to each of the VMs.

Idea is that the script function that stores credentials uses read-host to prompt for hostname then get-credentials to pick up domain/user/password.

The pass is then converted from secure-string using 256-bit key (runtime key unique to machine/user that stored the creds and runs the group launch).

The VMs name, domain, user and encrypted pass are stored in a file. When launching a session, the details are read in, password decrypted, details passed to cmdkey.exe to store \generic:TERMSRV credential for that VM, clear plaintext pass variable, launch mstsc to that host, a few seconds later remove the credential from windows credential store.
(If I passed password to cmdkey.exe as anything other than plaintext, the RDP session would either receive incorrect or no credentials).

So, hence the question, I need the password In plaintext to exist in memory for as short a time as possible.

To keep security guys happy, the script itself is aes256 encrypted and a c# wrapper with its own ps host reads, decrypts and runs the script, so there is no plaintext source on the machine that runs this. (Encrypted source on a file share so effectively I have a kill switch, can simply replace encrypted script with another displaying a message that this app has been disabled)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:17:47+00:00Added an answer on June 15, 2026 at 7:17 pm

    You can use a stopwatch to get the execution time for the commandlets. I think there is not really a time difference between these two cmdlets. I´m using normally “Remove-Item” because in my eyes it´s better to remove a variable complete.

    $a = "TestA"
    $b = "TestB"
    $c = "TestC"
    $d = "TestD"
    
    $time = New-Object system.Diagnostics.Stopwatch  
    
    Start-Sleep 1
    $time.Start() 
    $time.Stop()
    $system = $time.Elapsed.TotalMilliseconds
    Write-Host "Stopwatch StartStop" $system
    $time.Reset()
    
    Start-Sleep 1
    $time.Start() 
    Clear-Item Variable:a
    $time.Stop()
    $aTime = $time.Elapsed.TotalMilliseconds - $system
    Write-Host "Clear-Item in " $aTime
    $time.Reset()
    
    Start-Sleep 1
    $time.Start() 
    Remove-Variable b
    $time.Stop()
    $bTime  = $time.Elapsed.TotalMilliseconds - $system
    Write-Host "Remove-Variable in " $bTime
    $time.Reset()
    
    Start-Sleep 1
    $time.Start() 
    Clear-Item Variable:c
    $time.Stop()
    $cTime = $time.Elapsed.TotalMilliseconds - $system
    Write-Host "Clear-Item in " $cTime
    $time.Reset()
    
    Start-Sleep 1
    $time.Start() 
    Remove-Variable d
    $time.Stop()
    $dTime  = $time.Elapsed.TotalMilliseconds - $system
    Write-Host "Remove-Variable in " $dTime
    $time.Reset()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to think of the most efficient way to search a directory full
I have a text file which I am reading and storing the data in
I am storing a list of numbers (as Double) in a text file, then
I'm using CF8 and SQL2000. I'm storing a bunch of HTML in a Text
Storing the current_user in a string after they submit a comment <%= f.text_field :commenter,
String match = hello; String text = 0123456789hello0123456789; int position = getPosition(match, text); //
According to the docs String text = String.format(%5.1e, 3.1415f); Should produce something like 3.1E+00
I have string: text and something and something like f213 @@ -1,9 +1,11 @@
public class Text extends JPanel { private String text; public Text() { this.setPreferredSize(new Dimension(20,20));
So I essentially need to do this: String text = line1\n; text += line2\n;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.