Create PSCredential without a password
Solution: $mycreds = New-Object System.Management.Automation.PSCredential (“username”, (new-object System.Security.SecureString))
Solution: $mycreds = New-Object System.Management.Automation.PSCredential (“username”, (new-object System.Security.SecureString))
There is a great article on TechNet from the Hey, Scripting Guy series that goes over a situation very similar to what you are describing: Renaming a computer and resuming the script after reboot. The magic is to use the new workflows that are part of version 3: workflow Rename-And-Reboot { param ([string]$Name) Rename-Computer -NewName … Read more
You can’t read and write to the same file while it’s open. Get-Content opens the file for reading and, at the same time, Set-Content tries to write to it. Put the Get-Content call in parentheses; it will open the file, read its content and close it. (Get-Content $FileFullpath) | …
There is no built-in solution in PowerShell V1 / V2. You will want to use the .NET System.Collections.Specialized.OrderedDictionary: $order = New-Object System.Collections.Specialized.OrderedDictionary $order.Add(“Switzerland”, “Bern”) $order.Add(“Spain”, “Madrid”) $order.Add(“Italy”, “Rome”) $order.Add(“Germany”, “Berlin”) PS> $order Name Value —- —– Switzerland Bern Spain Madrid Italy Rome Germany Berlin In PowerShell V3 you can cast to [ordered]: PS> [ordered]@{“Switzerland”=”Bern”; “Spain”=”Madrid”; … Read more
Limit just some files => pipe to Select-Object -first 10 Order in descending mode => pipe to Sort-Object LastWriteTime -Descending Do not list directory => pipe to Where-Object { -not $_.PsIsContainer } So to combine them together, here an example which reads all files from D:\Temp, sort them by LastWriteTime descending and select only the … Read more
$Password is a Securestring, and this will return the plain text password. [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))
In your particular case what you want is Tree /f. You have a comment asking how to strip out the part at the front talking about the volume, serial number, and drive letter. That is possible filtering the output before you send it to file. $Path = “C:\temp” Tree $Path /F | Select-Object -Skip 2 … Read more
You basically have 3 options to prevent the PowerShell Console window from closing, that I describe in more detail on my blog post. One-time Fix: Run your script from the PowerShell Console, or launch the PowerShell process using the -NoExit switch. e.g. PowerShell -NoExit “C:\SomeFolder\SomeScript.ps1” Per-script Fix: Add a prompt for input to the end … Read more
The documentation for try-catch-finally says: A Finally block runs even if you use CTRL+C to stop the script. A Finally block also runs if an Exit keyword stops the script from within a Catch block. See the following example. Run it and cancel it by pressing ctrl-c. try { while($true) { “Working..” Start-Sleep -Seconds 1 … Read more
When you use a relative path, it is based off the currently location (obtained via Get-Location) and not the location of the script. Try this instead: $ScriptDir = Split-Path -parent $MyInvocation.MyCommand.Path Import-Module $ScriptDir\..\MasterScript\Script.ps1 In PowerShell v3, you can use the automatic variable $PSScriptRoot in scripts to simplify this to: # PowerShell v3 or higher #requires … Read more