How to quietly remove a directory with content in PowerShell
Remove-Item -LiteralPath “foldertodelete” -Force -Recurse or, with shorter version rm /path -r -force
Remove-Item -LiteralPath “foldertodelete” -Force -Recurse or, with shorter version rm /path -r -force
Using .NET’s UTF8Encoding class and passing $False to the constructor seems to work: $MyRawString = Get-Content -Raw $MyPath $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False [System.IO.File]::WriteAllLines($MyPath, $MyRawString, $Utf8NoBomEncoding)
You almost had it with Not. It should be: if (-Not (Test-Path C:\Code)) { write “it doesn’t exist!” } You can also use !: if (!(Test-Path C:\Code)){} Just for fun, you could also use bitwise exclusive or, though it’s not the most readable/understandable method. if ((test-path C:\code) -bxor 1) {write “it doesn’t exist!”}
$ErrorActionPreference = “Stop” will get you part of the way there (i.e. this works great for cmdlets). However for EXEs you’re going to need to check $LastExitCode yourself after every exe invocation and determine whether that failed or not. Unfortunately I don’t think PowerShell can help here because on Windows, EXEs aren’t terribly consistent on … Read more
Use (V3 version): (Get-Content c:\temp\test.txt).replace(‘[MYID]’, ‘MyValue’) | Set-Content c:\temp\test.txt Or for V2: (Get-Content c:\temp\test.txt) -replace ‘\[MYID\]’, ‘MyValue’ | Set-Content c:\temp\test.txt
This could be due to the current user having an undefined ExecutionPolicy. In PowerShell as Administrator, you could try the following: Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
try using this one [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Invoke-WebRequest -Uri https://apod.nasa.gov/apod/
If the current console is not elevated and the operation you’re trying to do requires elevated privileges then you can start powershell with the Run as Administrator option : PS> Start-Process powershell -Verb runAs Microsoft Docs: Start-Process
Use: ii . which is short for Invoke-Item . It is one of the most common things I type at the PowerShell command line.
Tools are just tools. They help or they don’t. You need help or you don’t. If you know Unix and those tools do what you need them to do on Windows – then you are a happy guy and there is no need to learn PowerShell (unless you want to explore). My original intent was … Read more