Search code examples
powershellpowershell-remotinguser-profile

New-PsSession without creating a profile?


I need to use New-PsSession to run multiple commands on a remote computer to gather information from the registry.

The problem is when I run New-PsSession, it creates a user profile with my username on the computer and after I disconnect and remove the session, the profile is correctly "unloaded" but the files stay in C:\USERS\XXX.

This is a problem because sometimes my collegues who reimage computers will see my user profile on that computer and think I'm logging on that computer even though I just ran a few commands to check registry settings.

My question: Is there a way to create a PsSession without it creating a user profile to your name on the target computer or some other way to make it clean itself up after it's disconnected and removed?

Example code to illustrate the situation:

New-PSSession -ComputerName XXXX -name TEST

start-sleep 3 

Disconnect-PSSession -Name TEST 
Remove-PSSession -Name TEST

Of course I could do something like this after the PsSession is removed:

Get-WMIObject -ComputerName X -class Win32_UserProfile | Where {$_.LocalPath -eq "C:\Users\X" } | Remove-WmiObject

It works but it feels "dirty". Am I missing something?


Solution

  • Have you tried the following:

    $option = New-PSSessionOption -nomachineprofile
    New-PSSession -ComputerName XXXX -name TEST -SessionOption $option