((Get-Content -path '\\windows7\c$\test.txt' -Raw) -replace ' username', 'white') | Set-Content -Path '\\windows7\C$\test.txt'
PS C:\> C:\changestring.ps1
New-PSDrive : The user name or password is incorrect
At C:\changestring.ps1:3 char:18
+ $windows7Drive = New-PSDrive -Name Windows7Drive -PSProvider FileSystem -Root "\ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Windows7Drive:PSDriveInfo) [New-PSDrive], Win32Exception
+ FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand
$username = 'JDock'
to$username = 'DomainName\JDock'
New-PSDrive : Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed
$username = 'JDock'
## Run the following command on the computer where you want to store the script, and with the user who executes the script.
## It will copy the complete PowerShell command into the clipboard, from where you can paste it and replace the "$password = ..."
## 'Passw0rd' | ConvertTo-SecureString -AsPlaintext -Force | ConvertFrom-SecureString | % {"`$password = '$($_)'"} | clip.exe
$password = '0101...'
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, (ConvertTo-SecureString -String $password)
ConvertTo-SecureString : Input string was not in a correct format.
At C:\changestring.ps1:13 char:104
+ ... st $username, (ConvertTo-SecureString -String $password)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [ConvertTo-SecureString], FormatException
+ FullyQualifiedErrorId : System.FormatException,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand
New-PSDrive : Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all
previous connections to the server or shared resource and try again
At C:\changestring.ps1:14 char:18
+ $windows7Drive = New-PSDrive -Name Windows7Drive -PSProvider FileSystem -Root "\ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Windows7Drive:PSDriveInfo) [New-PSDrive], Win32Exception
+ FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand
PS C:\>
$username = 'JDock'
$password='Passw0rd'
ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, (ConvertTo-SecureString -String $password)
$windows7Drive = New-PSDrive -Name Windows7Drive -PSProvider FileSystem -Root "\\windows7\C$" -Credential $credential -ErrorAction Stop
((Get-Content -Path '\\windows7\c$\test.txt' -Raw) -replace ' username', 'white') | Set-Content -Path '\\windows7\C$\test.txt'
$windows7Drive | Remove-PSDrivecls
You can get the credentials using Get-Credential.
Note that you don't need to connect with the logged no user; you just need to connect with any account that has local administrator privileges on the remote machine.
Open in new window