OneCompiler

How to restart a computer using powershell?

I have windows running on ec2 instance, I want to turn off the system using Powershell. How can I do that?

2 Answers

5 years ago by

Run this command,

Restart-Computer -ComputerName "Server01" -Wait -For PowerShell -Timeout 600 -Delay 2
5 years ago by Divya

=============================================

Script: Criar Usuario Scan Oculto + Pasta Compartilhada

=============================================

Write-Host ""
Write-Host "======================================" -ForegroundColor Cyan
Write-Host " Configurando usuario Scan..." -ForegroundColor Cyan
Write-Host "======================================" -ForegroundColor Cyan
Write-Host ""

1. Criar o usuario Scan

$Senha = ConvertTo-SecureString "1234" -AsPlainText -Force

try {
New-LocalUser -Name "Scan" -Password $Senha -FullName "Scan" -Description "" -PasswordNeverExpires -ErrorAction Stop
Write-Host "[OK] Usuario Scan criado com sucesso!" -ForegroundColor Green
} catch {
Write-Host "[AVISO] Usuario Scan ja existe, continuando..." -ForegroundColor Yellow
}

Adicionar ao grupo Usuarios

Add-LocalGroupMember -Group "Users" -Member "Scan" -ErrorAction SilentlyContinue
Write-Host "[OK] Usuario adicionado ao grupo Usuarios!" -ForegroundColor Green

2. Ocultar o usuario da tela de login

$RegistroPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList"

if (-not (Test-Path RegistroPath)) { New-Item -Path RegistroPath -Force | Out-Null
}

Set-ItemProperty -Path $RegistroPath -Name "Scan" -Value 0 -Type DWord
Write-Host "[OK] Usuario Scan ocultado da tela de login!" -ForegroundColor Green

3. Criar a pasta em Documentos do sistema

NomeComputador=NomeComputador = env:COMPUTERNAME
$CaminhoPasta = "C:\Users\Public\Documents\ScanCompartilhado"

if (-not (Test-Path CaminhoPasta)) { New-Item -ItemType Directory -Path CaminhoPasta -Force | Out-Null
Write-Host "[OK] Pasta criada em: $CaminhoPasta" -ForegroundColor Green
} else {
Write-Host "[AVISO] Pasta ja existe, continuando..." -ForegroundColor Yellow
}

4. Definir permissoes exclusivas para o usuario Scan

icacls CaminhoPasta/inheritance:rOutNullicaclsCaminhoPasta /inheritance:r | Out-Null icacls CaminhoPasta /grant "Scan:(OI)(CI)F" | Out-Null
icacls $CaminhoPasta /remove "BUILTIN\Users" | Out-Null
Write-Host "[OK] Permissoes configuradas apenas para Scan!" -ForegroundColor Green

5. Criar o compartilhamento de rede

$CompartilhamentoExiste = Get-SmbShare -Name "Scan" -ErrorAction SilentlyContinue

if ($CompartilhamentoExiste) {
Remove-SmbShare -Name "Scan" -Force
Write-Host "[AVISO] Compartilhamento antigo removido, recriando..." -ForegroundColor Yellow
}

New-SmbShare -Name "Scan" -Path $CaminhoPasta -FullAccess "Scan" | Out-Null
Write-Host "[OK] Pasta compartilhada na rede!" -ForegroundColor Green

6. Exibir resultado final

Write-Host ""
Write-Host "======================================" -ForegroundColor Cyan
Write-Host " CONFIGURACAO CONCLUIDA!" -ForegroundColor Cyan
Write-Host "======================================" -ForegroundColor Cyan
Write-Host ""
Write-Host " Usuario: Scan" -ForegroundColor White
Write-Host " Senha: 1234" -ForegroundColor White
Write-Host " Pasta local: CaminhoPasta"ForegroundColorWhiteWriteHost"Caminhoderede:CaminhoPasta" -ForegroundColor White Write-Host " Caminho de rede: \\NomeComputador\Scan" -ForegroundColor White
Write-Host ""
Write-Host " Use esse caminho na impressora HP:" -ForegroundColor Yellow
Write-Host " \$NomeComputador\Scan" -ForegroundColor Yellow
Write-Host ""
Write-Host "======================================" -ForegroundColor Cyan