Hide Powershell Commands Out Put and popup Windows

This topic contains 7 replies, has 2 voices, and was last updated by Profile photo of skl816405 skl816405 2 weeks, 4 days ago.

  • Author
    Posts
  • #72172
    Profile photo of skl816405
    skl816405
    Participant

    Hi there

    Is there a way to hide all the commands out puts and the popup windows of my script to format a USB disk.
    I have used ( | Out-Null ) it help't a little but it didn't hide everything

    Best regards to you all

    This is my script
    ======================================================================================================================
    ======================================================================================================================

    # Get Admin Rights

    New-Item 'HKCU:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' -Force | New-ItemProperty -Name ExecutionPolicy -Value Unrestricted -Force | Out-Null

    if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit } | Out-Null

    # Run Secrit

    $USBDriveNmbr = GET-WMIOBJECT win32_diskdrive | Where { $_.InterfaceType –eq 'USB' } | Select -Expand DeviceID
    $USBDriveNmbr = $USBDriveNmbr.Trim("\\.\PHYSICALDRIVE")
    $USBDriveLtr1 = (Get-WmiObject Win32_Volume -Filter "DriveType='2'").DriveLetter.Substring(0,1)
    $USBDriveLtr2 = Get-WmiObject Win32_Volume -Filter "DriveType='2'"|select -expand driveletter

    Get-Disk | Out-Null

    Clear-Disk $USBDriveNmbr -RemoveData -Confirm:$False | Out-Null

    New-Partition -DiskNumber $USBDriveNmbr –UseMaximumSize -IsActive:$true -AssignDriveLetter | Out-Null

    Format-Volume -DriveLetter $USBDriveLtr1 -FileSystem NTFS -NewFileSystemLabel WinSetup | Out-Null

    Bootsect.exe /NT60 $USBDriveLtr2 | Out-Null

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

  • #72196
    Profile photo of Chris
    Chris
    Participant

    Hi Skl,

    You can achieve this two ways, you can either input some arguments on the program properties (target)

     powershell.exe yourpsscript.ps1 -windowstyle hidden 

    Or you can run it from a batch file, with those arguments inside,

     C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -WindowStyle Hidden -file "yourpsscript.ps1" 

    Either should work, there are possibly ways to incorperate it into the PS Script itself, but i imagine it will pop up ever so briefly if this is the case.

  • #72203
    Profile photo of skl816405
    skl816405
    Participant

    Hi Chris

    i'm grateful for your answer, but what i want is to have it inside the same script.

    thank you again Chris

  • #72206
    Profile photo of Chris
    Chris
    Participant

    This should help,

    Put it at the top of your Script, but you will briefly (a second) see a window appear, it's probably the closest you are going to get to a hidden window.

     
    Add-Type -Name win -MemberDefinition '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);' -Namespace native
    [native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle,0)
    
  • #72214
    Profile photo of skl816405
    skl816405
    Participant

    It didn't work, i get error message

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

    At C:\Users\Admin\AppData\Local\Temp\1\451F.ps1:8 char:276
    + ... Policy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit } | Out-Nul ...
    + ~
    An empty pipe element is not allowed.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : EmptyPipeElement

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

  • #72218
    Profile photo of Chris
    Chris
    Participant

    Hi Skl,

    That looks like an error in the code you have already defined, as above? the line

     if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit } | Out-Null
    

    Does that run without errors before the snipped is inserted? Try removing the Out-Null and the pipe.

  • #72223
    Profile photo of skl816405
    skl816405
    Participant

    Now i didn't get error message and i didn't get windows popups, but the script didn't work correctly, it deleted the partition and it didn't create the new partition.

    so what i have now a usb disk without partition.

  • #72224
    Profile photo of skl816405
    skl816405
    Participant

    hi Chris

    Before adding your script everything worked well, just a lots of popups and you could see a lot of codes.

    and i thought if theirs is away to hide it all.

    thank you very much for your help.

You must be logged in to reply to this topic.