Start 3 windows services at a time with delay between them - windows

I am new to PowerShell and I need to write a script to start 3 or more windows services, in a specific order, with a set delay between them. I know how to write the script to start all of them, but I have no idea how to set the delay:
Get-Service -ComputerName cumputer_name -Name service_name1 | Stop-Service -Verbose
Get-Service -ComputerName cumputer_name -Name service_name2 | Stop-Service -Verbose

I believe you are looking for Start-Sleep.
Get-Service -ComputerName cumputer_name -Name service_name1 | Stop-Service -Verbose
Start-Sleep -Seconds 5
Get-Service -ComputerName cumputer_name -Name service_name2 | Stop-Service -Verbose

Related

Powershell script to disable all Windows 10 Notifications?

Is there a way to disable Action Center Notifications in Windows 10 with PowerShell?
I found a way with 2 registry keys,
DisableNotificationCenter and ToastEnabled.
New-Item -Path "HKCU:\Software\Policies\Microsoft\Windows" -Name "Explorer" -force
New-ItemProperty -Path "HKCU:\Software\Policies\Microsoft\Windows\Explorer" -Name "DisableNotificationCenter" -PropertyType "DWord" -Value 1
New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\PushNotifications" -Name "ToastEnabled" -PropertyType "DWord" -Value 0
This works with PowerShell 5 on Windows 10 EN, Version 21H2.

How to combine TYPE and DIR command in windows cmd?

I have to translate a LINUX command into a Windows one. This is the command:
cat $(find folderName)/fileName
Searching online I found that:
cat can be translated with type
find can be translated with dir
So I tried to use something like this:
type #(dir folderName /s)/fileName
type < (dir folderName /s)/fileName
They are wrong, but I can't find any solution to combine them.
Can someone help me?
I am confident that others will provide FOR loop solutions.
This is not difficult if you use PowerShell Core or Windows PowerShell. If you are on a supported Windows system, Windows PowerShell was installed with it and is available.
While you might rightly say that this is more typing than needed in a bash/ksh script, it will run equally well on Linux, MacOS, and Windows without any modification. No 'translate' needed.
Get-Content -Path $(Get-ChildItem -Recurse -File -Path '.' -Filter 'sheet1.xml' |
Where-Object { $(Split-Path -Path $_.DirectoryName -Leaf) -eq 'worksheets' }).FullName
The amount of typing can be reduced by using aliases. While that is ok at an interactive shell, it is bad practice to encode aliases into script files.
gc $(gci -rec -file 'sheet1.xml'|?{$(Split-Path $_.DirectoryName -Leaf) -eq 'worksheets' }).FullName
If you are desperate to run this from a cmd command prompt, the following could be used.
powershell -NoLogo -NoProfile -Command ^
Get-Content -Path $(Get-ChildItem -Recurse -File -Path '.' -Filter 'sheet1.xml' ^| ^
Where-Object { $(Split-Path -Path $_.DirectoryName -Leaf) -eq 'worksheets' }).FullName
powershell -NoLogo -NoProfile -Command ^
gc $(gci -rec -file 'sheet1.xml'^|?{$(Split-Path $_.DirectoryName -Leaf) -eq 'worksheets' }).FullName

powershell New-PSDrive consistency

I created a script that runs from local pc and uses invoke-command to copy files from Host-1 to host-2.
the Relevant part:
$session =New-PSSession -Computername $Thost -Credential $mycreds
Invoke-Command -Session $session -ScriptBlock {
#seting a temp drive on Host for simple approch
Remove-PSDrive -Name w -Force
New-PSDrive -Name w -PSProvider FileSystem -Root $Using:VMPath -Credential $Using:mycreds -ErrorAction Stop
Write-Host "You Chose that $Using:OSVersion will be copyed to $Using:Thost"
Copy-Item -Path "w:\$Using:OSVersion" -Destination $using:VMXPath -Recurse
Remove-PSDrive -Name w -Force -ErrorAction Stop
}
first time I run the script it runs great!
First Run of the Script
the second time it says the PSdrive doesn't exist.
Second Run Error in PowerShell
after 10 min +- the script runs again with no issues
I'm surprised it runs the first time through. It looks like the failure is on the initial Remove-PSDrive. Its failing because you've already removed the w drive.
Adopt one of these 2 models
Remove-PSDrive -Name w -ErrorAction SilentlyContinue
New-PSDrive -Name w -PSProvider FileSystem -Root c:\test
Remove-PSDrive -Name w -Force
OR
if (Test-Path -Path w:\){
Remove-PSDrive -Name w -Force
}
New-PSDrive -Name w -PSProvider FileSystem -Root c:\test
Remove-PSDrive -Name w -Force
I'd go with the second option for preference

PowerShell Script to Remove Registry Item

I'm trying to make a PowerShell script that checks a set registry key for a range of names that start the same. That part I have working fine. I also need this script to than remove those items from that registry and I am having trouble remembering how to pass the names of all items I find so that Remove-ItemProperty will work. This is what I have.
$Reg = 'HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Devices'
Get-ItemProperty -Path $Reg | Select-Object IS* | ForEach-Object {$PSItem.Name} | Remove-ItemProperty -Path $Reg -Name $name
The message I get is that Name is null so I'm not storing the names correctly. They display correctly if I just run the first two pipes.
Try this. Had to re-write a bit to make the property name stick.
Get-Item -Path "$Reg" | Select-Object -ExpandProperty Property |
ForEach-Object {if ($_ -match "IS*"){Remove-ItemProperty -Path "$Reg" -Name "$_"}}

Get application/program which created a file in PowerShell

I have several programs (.exe) installed on my computer which create ".dat" files. One of them creates very large files.
I want to delete only the files created by this program.
Is there a way in PS to know which program has created a file?
I try to filter them in following code:
Get-ChildItem $path -Filter "*.dat" -Recurse -Force |'
Where-Object {$_.CreationTime -lt $limit -and $_.LastAccessTime -lt $limit2 -and $_.Attributes -notmatch "Offline" } |'
Remove-Item -Force
This is not possible unless your program somehow stamps a property onto .dat file which you would then read.
Use Process Explorer if you want to find out which executable creates the file in question.
One option is to execute the ".exe programms" under different credentials, one different user per exe. Then you can use get-acl to identify who created each file:
$OWNER = "xxxx"
Get-ChildItem $path -Filter "*.dat" -Recurse -Force |'
Where-Object {Get-Acl $_.FullName -eq $OWNER -and $_.CreationTime -lt $limit -and $_.LastAccessTime -lt $limit2 -and $_.Attributes -notmatch "Offline" } |'
Remove-Item -Force
I would suggest using ProcessMonitor ( http://technet.microsoft.com/en-gb/sysinternals/bb896645). This will show all file system activity and will quickly show you which processes are accessing which files.

Resources