recentpopularlog in

kme : powershell   68

Hashing it Out in PowerShell: Using Get-FileHash | https://www.petri.com/
Windows 7, even with all the updates, only has PowerShell 2.0, so no 'get-filehash' for Windows 7.
PowerShell 4.0 introduced a new cmdlet, Get-FileHash, primarily for use with Desired State Configuration (DSC). In a pull server configuration, you need to provide file hashes so that servers can recognize changes. That is the primary purpose of a file hash as far as I am concerned is for file integrity. Windows supports several different hashing algorithms, which you should not confuse with encryption.
powershell  hash  checksum  crypto  windows  win7  shellscripting 
7 weeks ago by kme
lucasg/powershell-docset: A dash docset for powershell modules : https://docs.microsoft.com/en-us/powershell/module/ | https://github.com/
Get a release, and then extract the .tgz for a specific version, then move the "Powershell.docset" directory into ~/.local/share/Zeal/Zeal
zeal  dash  velocity  docset  powershell  devel  reference  solution 
7 weeks ago by kme
How to add separator in powershell - Stack Overflow
Basically, use 'export-csv' or 'converto-csv' with a '-delimiter' option.
csv  windows  powershell  shellscripting  textprocessing  newbie  solution 
7 weeks ago by kme
How do I concatenate strings and variables in PowerShell? - Stack Overflow
This was my solution:

<code class="lang-powershell">gci -recurse -include *.exe,*.msi |
%{ get-filehash $_ -a SHA1 } |
%{ write-host $(
$_.hash + " " +
$(Resolve-Path -Relative $_.path | %{$_ -split "\\" -join "/"})
)
}</code>
powershell  stringmanipulation  textprocessing  shellscripting  syntax  solution 
7 weeks ago by kme
.net - What does $_ TRULY mean in PowerShell? - Stack Overflow
The $_ is only created in the context of a pipeline, that is the passing of output objects from one command into the "input" of another. For pipeline filters (which are like functions), powershell automatically populates the $_ variable with the current object of the pipeline. Now I say current because it is important to note that when multiple objects are passed down the pipeline, the process scriptblock of your filter is executed once per object.
windows  powershell  shellscripting  iteration  pipes  syntax  newbie  solution 
7 weeks ago by kme
syntax - What does "%" (percent) do in PowerShell? - Stack Overflow
When used in the context of a cmdlet (such as your example), it's an alias for ForEach-Object:
windows  powershell  shellscripting  syntax  newbie  solution 
7 weeks ago by kme
The Complete Guide to PowerShell Punctuation - Simple Talk
Backtick is backslash, basically, including for line continuation. Bad choice if you ask me, because it's not visually distinctive enough. It looks like crumbs in your code.
powershell  windows  shellscripting  syntax  grammar  explained  reference  newbie 
7 weeks ago by kme
How to get a comma separated table of installed Windows programs using PowerShell? - Stack Overflow
<code class="lang-powershell">Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, DisplayVersion |
ConvertTo-Csv -NoTypeInformation</code>
windows  sysadmin  softwaremanagement  powershell  shellscripting  solution 
7 weeks ago by kme
In Powershell, how do I concatenate one property of an array of objects into a string? - Stack Overflow | https://stackoverflow.com/
What I was actually looking for was like Awk with a specific RS. I ended up using `write-host` in a subexpression, something like this:

<code class="language-powershell">
gci -recurse -include *.exe,*.msi |
%{ get-filehash $_ -a SHA1 } |
%{ write-host $(
$_.hash + " " +
$(Resolve-Path -Relative $_.path | %{$_ -split "\\" -join "/"})
)
}
</code>

I think either 'convertto-csv' or 'export-csv' with '-delimiter' might work, though.

Note that 'get-filehash' doesn't seem to work on Windows 7 (as shipped, even with all current updates). It was introduced in PowerShell 4.0, and who knows what OS got that first (8, 10?).
windows  shellscripting  powershell  stringmanipulation  textprocessing  sortof  solution 
7 weeks ago by kme
PowerShell command to split from output - Stack Overflow | https://stackoverflow.com/
<code class="language-powershell">
Get-Content Carlist.txt | select-string carId | Foreach-Object { $_.ToString().split(':')[1] -replace '\s','' }
</code>
powershell  windows  textprocessing  shellscripting  solution 
7 weeks ago by kme
encoding - Using PowerShell to write a file in UTF-8 without the BOM - Stack Overflow | https://stackoverflow.com/
<code class="language-powershell">
Get-Content path/to/file.ext | out-file -encoding ASCII targetFile.ext
</code>
utf16  utf8  bom  utf  unicode  powershell  textprocessing  solution 
7 weeks ago by kme
10 Ways to Open the Command Prompt in Windows 10 | https://www.howtogeek.com/
Right-clicking on the Start menu now shows 'PowerShell' instead of 'Command Prompt' options, as of the "Creators" update.
windows  sysadmin  powershell  commandprompt  cmd  newbie  howto 
7 weeks ago by kme
powershell - Get relative path of files in sub-folders from the current directory - Stack Overflow
The Resolve-Path cmdlet has a -Relative parameter that will return a path relative to the current directory:
<code class="language-powershell">
Set-Location C:\MyScript
$relativePath = Get-Item Data\Test.txt | Resolve-Path -Relative
</code>
powershell  relativepathname  shellscripting  scripting  windows  solution 
7 weeks ago by kme
move - Get-ChildItem results looks like relative paths in Powershell - Stack Overflow
A useful example of 'foreach':
<code class="language-powershell">
Get-ChildItem -recurse -path sub\WORK -filter "* OK" |
Where-Object { $_.PSIsContiner } |
foreach { Move-Item -path $_.Fullname -destination sub\OK }
</code>
powershell  shellscripting  scripting  windows  relativepathnames  samplecode 
7 weeks ago by kme
windows - Run Python scripts in PowerShell directly - Super User
Edit the PATHEXT environment variable and add the .py extension.

Just add this line to your powershell profile:
<code class="language-powershell">$env:PATHEXT += ";.py"</code>

or you could just edit PATHEXT globally using the system settings (just start menu search for "Edit Environment variables for your account").


See https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles; the MS TechNet article linked in the approved answer is looooong dead.
python  powershell  windows  cli  commandline  pathext  path  environmentvariable  shellscripting  solution  essential  movein  configuration 
august 2019 by kme
windows - How do I use a pipe to redirect the output of one command to the input of another? - Stack Overflow
So it looks like you can't pipe the output of batch files into other programs with CMD.EXE, which I just... never noticed.

Workaround is to use PowerShell to run the whole pipeline, since it has proper pipes
You can also run exactly same command at Cmd.exe command-line using PowerShell. I'd go with this approach for simplicity...
<code class="language-powershell">PowerShell -Command "temperature | prismcom.exe usb"</code>

Please read up on Understanding the Windows PowerShell Pipeline

You can also type in C:\>PowerShell at the command-line and it'll put you in PS C:\> mode instanctly, where you can directly start writing PS.
windows  cli  cmd.exe  cmd  shellscripting  powershell  workaround  sortof  solution 
august 2019 by kme
Access clipboard in Windows batch file - Stack Overflow
tl;dr:
<code class="language-powershell">get-clipboard</code>

(works on Windows 10 at least)

The alternatives are ghastly:
Slimming it down (on a new enough version of Windows):
<code class="language-powershell">set _getclip=powershell "Add-Type -Assembly PresentationCore;[Windows.Clipboard]::GetText()"
for /f "eol=; tokens=*" %I in ('%_getclip%') do set CLIPBOARD_TEXT=%I</code>

- First line declares a powershell commandlet.
- Second line runs and captures the console output of this commandlet into the CLIPBOARD_TEXT enviroment variable (cmd.exe's closest way to do bash style backtick ` capture)


(and that was one of the _concise_ solutions)
windows  powershell  cli  shellscripting  clipboard  automation  ayfkm  microsoft  whynotpowershell  alternativeto  xclip  pbcopy  pbpaste 
august 2019 by kme
cmd - How can you get the clipboard contents with a Windows command? - Stack Overflow
<code class="language-dos">powershell -command "Get-Clipboard" > file.txt</code>

This cannot be stored in a DOSKEY macro, because those can't be used in pipes.
windows  cmd  powershell  clipboard  cli  automation  pipes  workaround  sortof  solution 
august 2019 by kme
How to Create a List of Your Installed Programs on Windows
Astonished that this is the first useful use case I've found for PowerShell so far.

<code style="language-powershell">Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize</code>
sysadmin  win7  powershell  installation  maintenance  reporting  commandline  cli  fuckina  solution 
july 2018 by kme
How to Extract ZIP Files Using PowerShell [https://www.howtogeek.com/]
function Expand-ZIPFile($file, $destination)
{
$shell = new-object -com shell.application
$zip = $shell.NameSpace($file)
foreach($item in $zip.items())
{
$shell.Namespace($destination).copyhere($item)
}
}
windows  sysadmin  powershell  scripting  compression  archive  maybesolution 
august 2017 by kme
Script Download files from websites programatically via powershell [https://gallery.technet.microsoft.com/]
$url = "http://website.com/downloads/Iwantthisfile.txt"
$path = "C:\temp\thisisthefile.txt"
# param([string]$url, [string]$path)

if(!(Split-Path -parent $path) -or !(Test-Path -pathType Container (Split-Path -parent $path))) {
$path = Join-Path $pwd (Split-Path -leaf $path)
}

"Downloading [$url]`nSaving at [$path]"
$client = new-object System.Net.WebClient
$client.DownloadFile($url, $path)
#$client.DownloadData($url, $path)

$path
powershell  scripting  webdevel  maybesolution 
august 2017 by kme
cmd - Can I mask an input text in a bat file - Stack Overflow
Best solution: use a Powershell one-liner as a shim to get masked input
@echo off
@echo off
set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%p
echo %password%


Alternate solution (you'll need http://blog.magerquark.de/scriptpw-dll-on-windows-2008-and-windows-7-scriptpw-password/ if >= Win7):
@echo off
:: GetPwd.cmd - Get password with no echo.
<nul: set /p passwd=Password:
for /f "delims=" %%i in ('cscript /nologo GetPwd.vbs') do set passwd=%%i
echo.
:: This bit's just to prove we have the password.
echo %passwd%

The "<nul: set /p passwd=Password: " is somewhat sneaky - the effect of the command is to output the prompt with no trailing newline character - it's a sneaky way to emulate the "echo -n" command from the bash shell. It sets passwd to an empty string as an irrelevant side effect and doesn't wait for input since it's taking its input from the nul: device.


PowerShell solution:
@echo off
set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%p
echo %password%
windows  vbscript  powershell  dos  batchfile  batch  scripting  tipsandtricks  passwordprompt  authentication  solution 
october 2014 by kme

Copy this bookmark:





to read