powershell
How can I specify the column ordering when exporting to CSV in PowerShell?
gvee’s helpful answer is a pragmatic solution that ensures that the columns appear in the desired order, because the order in which you pass property names to Select-Object is the order in which the properties are added to the resulting [pscustomobject] instances. It is, however, inefficient, because the desired column order can be ensured at … Read more
Start-Process -WorkingDirectory as administrator does not set location
I also had the same problem and solved it with this command: Start-Process powershell.exe -verb runAs -ArgumentList ‘-NoExit’, ‘-Command’, ‘cd D:\folder’ Once you run the above command, Windows will launch with admin authority and the specified directory.
How to determine if I’m in powershell or cmd?
All credit goes to PetSerAl, this had to be posted as an aswer: (dir 2>&1 *`|echo CMD);&<# rem #>echo PowerShell Within Win32-OpenSSH this command also works, and outputs CMD. NB : Win32-OpenSSH seems a bit limited, cd is not recognized on my system.
When to choose development of a PowerShell Module over PowerShell Script
To understand what modules can do for you, read this: https://learn.microsoft.com/en-us/powershell/scripting/developer/module/writing-a-windows-powershell-module?view=powershell-7.1 In a nutshell, Windows PowerShell modules allow you to partition, organize, and abstract your Windows PowerShell code into self-contained, reusable units. With these reusable units, administrators, script developers, and cmdlet developers can easily share their modules directly with others. Script developers can also repackage … Read more
Powershell: get output from Receive-Job
If the job uses Write-Host to produce output, Receive-Job returns $null, but the results get written to the host. However, if the job uses Write-Output to produce output in lieu of Write-Host, Receive-Job returns a string array [string[]] of the job output. To demonstrate, enter this innocuous code at the PowerShell prompt: $job = Start-Job … Read more
Powershell Copy-Item but only copy changed files
Both of those are solid answers for powershell, but it would probably be far more easy to just leverage Robocopy (MS Supplied robust copy application). robocopy “C:\SourceDir\” “C:\DestDir\” /MIR would accomplish the same thing.
Is there a way to specify a font color when using write-output
I have tried this extra function and it basically works fine: function Write-ColorOutput($ForegroundColor) { # save the current color $fc = $host.UI.RawUI.ForegroundColor # set the new color $host.UI.RawUI.ForegroundColor = $ForegroundColor # output if ($args) { Write-Output $args } else { $input | Write-Output } # restore the original color $host.UI.RawUI.ForegroundColor = $fc } # test … Read more
How can I call a file from the same folder that I am in (in script)
i use the powershell variable which has an easy name to remember what is it for. $PSScriptRoot\scriptb.ps1
Is there a PowerShell “string does not contain” cmdlet or syntax?
If $arrayofStringsNotInterestedIn is an [array] you should use -notcontains: Get-Content $FileName | foreach-object { ` if ($arrayofStringsNotInterestedIn -notcontains $_) { $) } or better (IMO) Get-Content $FileName | where { $arrayofStringsNotInterestedIn -notcontains $_}