Skip to content

Instantly share code, notes, and snippets.

@csharpforevermore
Created December 24, 2018 15:18
Show Gist options
  • Save csharpforevermore/bbccfd0e89b0acc0d1969e2da0b050b5 to your computer and use it in GitHub Desktop.
Save csharpforevermore/bbccfd0e89b0acc0d1969e2da0b050b5 to your computer and use it in GitHub Desktop.
A collection of PowerShell commands
# List Current Version
$PSVersionTable.PSVersion
Example 1: Send output to a file
Get-Process | Out-File -filepath C:\Test1\process.txt
# Example 2: How to retrieve recursively any files with a specific extensions in PowerShell?
# https://stackoverflow.com/questions/31049454/how-to-retrieve-recursively-any-files-with-a-specific-extensions-in-powershell
Get-ChildItem -Path .\ -Filter *.js -Recurse -File -Name| ForEach-Object {
[System.IO.Path]::GetFileNameWithoutExtension($_)
}
# Example 3a: Get appSettings key value in Powershell
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="updatelocation" value="\\gm107a\Updates\QC"/>
<add key="filename" value="QualityControl.exe"/>
</appSettings>
</configuration>
function vert {
$hostnamenodes = get-content C:\Scripts\Computers.txt
foreach ($hostname in $hostnamenodes) {
[xml]$xml = Get-Content \\$hostname\C$\Drivers\Version.config
#Add hostrecord to array
$MasterArray = New-Object psobject -Property @{
"ServerName" = $hostname
"updatelocation" = $xml.configuration.appSettings.add | Where-Object { $_.key -eq 'updatelocation' } | Select-Object -ExpandProperty value
}
write-output $masterarray
} }
vert | select servername,updatelocation | Export-Csv QA.csv -NoTypeInformation
# Example 3b:
# Example 1: Send output to a file
Get-Process | Out-File -filepath C:\Test1\process.txt
# Example 2: Send output to a file without overwriting
Get-Process | Out-File C:\Test1\process.txt -NoClobber
Out-File : File C:\Test1\process.txt already exists and NoClobber was specified.
At line:1 char:23
+ Get-Process | Out-File <<<< process.txt -NoClobber
Example 3: Send output to a file in ASCII format
$A = Get-Process
Out-File -FilePath C:\Test1\process.txt -InputObject $A -Encoding ASCII -Width 50
# Example 4: Send output from outside a file system drive
Set-Location hklm:\software
Get-Acl mycompany\mykey | Out-File -FilePath c:\ps\acl.txt
Get-Acl mycompany\mykey | Out-File -FilePath filesystem::acl.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment