• You have it correct Cornel.. on line 9, you just add to it.. 

    $folders = get-childitem -Directory -Path "somepath"
    $folders |
    ForEach-Object { 
            $Directory = $_.FullName
        [pscustomobject]@{
            Directory = $Directory
            FileCount = (Get-ChildItem -Path $Directory -Recurse -File).Count 
        }
    } | Export-CSV -filepath <path> -NoTypeInformation

    David F. 

  • You just need to use the correct .Net class..

    Add-Type -AssemblyName System.Windows.Forms
    $dialog = [System.Windows.Forms.FolderBrowserDialog]::new()
    $dialog.Description = 'This is a description'
    $dialog.RootFolder = [System.Environment+specialfolder]::Desktop
    $dialog.ShowNewFolderButton = $true
    $dialog.ShowDialog()
    
    $dialog.Dispose()

    The documentation is at https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.folderbrowserdialog?view=netframework-4.8.&nbsp;

    David F. 

  • Please provide powershell reboot script for multiple machines with 2alert options along with force reboot.

  • Very good blog and helpful. Similarly how can we select folder path instead of file?? 

    Thanks in Advance 🙂

     

  • David, easy to understand and very well explained with examples. Thanks!

    How can i export the results from an [pscustomobject] to a log file ( ie ExportCs,etc. Export-Csv prompt me for an InputObject)

  • David, easy to understand and very well explained with examples. Thanks!

    How can i export the results from a [pscustomobject] to a log file ( ie ExportCs,etc. Export-Csv prompt me for an InputObject)

  • This works a treat, thank you very much!

  • Love it but hate it. I haven't figured out how to get this to be successful for an IIS web cert.

  • Configuring a container host is a basic requirement for running containers. In this post, we will take a look at configuring a Windows Server 2019 container host with PowerShell.

  • Hi Mike,

    really good post here, I will definitely try that out. Do you think its also suitable for very small customers with just one DC?

    Cheers

  • Ikechukwu, there are plenty of scripts for that task out there, for example a script created by one of 4sysops.com member Luc Fullenwarth - https://gallery.technet.microsoft.com/scriptcenter/Audit-monitor-protect-and-c79a85b1

    You always need to have a list of computers that you need to query first, or get the list from your AD.

  • Hello ... Good details here. Thanks for providing such an insight, but I really need a powershell command to show all local admin users on my network. Is there any you can share with me ?

  • The guy before me had the VM  and FW configured wink

  • I dont consider it as a security risk if everything is configured well. PS-Remoting use Kerberos, you can enable it only in Domain profile or specify exact IPs from where you can run the tasks. Both on Windows FW or network FW level.

  • If you had time to configure FW for your specific management VM, then you also got time to enable Ps-Remoting which is one time action 🙂

    You can work as you wish, of course.

  • Let`s say you just got there and have no time to start making other adjustments, GPO`s and other things.

    As for the FW thing, I prefer to have a VM for administration purposes so all the ports needed are open.

    My script simply works , dirty, but works, for sure I could make it better , but for now it just fill`s the need.

    Feel free to make it better and share it . Thank You.

  • Leos, thanks for your response. Once enabling that on the remote machine, it worked! I'm assuming PSRemoting should be disabled as it could be a security risk?

  • Why are you using such script to start WinRM service? It is very unusual. Remember that the *-Service commands use WMI to run, which might be blocked on firewalls. Standard procedure is to have Powershell remoting enabled on all computers via GPO and then just use Invoke-command or other commands that run via WinRM.

  • #Computers to install fonts to
    $ComputerArray = @("Pc1","Pc2","Pc3")
    #A Share containing only the fonts you want to install
    $FontDir="FileServerSharesHelpdeskSpecialFonts"
    #Wil be created on remote Pc if not exists, fonts will be copied here and deleted after install.
    $PcStagingDir="c:tools"
    
    foreach ($pc in $ComputerArray) {
    $RemotePcStagingDir = "$pc$($PcStagingDir.replace(':','$'))"
    If(Test-Connection -ComputerName  $pc -Count  1 -ErrorAction SilentlyContinue) {
    $DisabledSvcs=@()
    $ServiceName = @("WinRM")
    foreach ($svc in $ServiceName) {
            $i=0
            while(((Get-Service -ComputerName $pc -Name $svc -ErrorAction SilentlyContinue).status -ne "Running")-and($i -lt 10)){
                if((Get-Service -ComputerName $pc -Name $svc -ErrorAction SilentlyContinue).StartType -eq "Disabled"){
                Write-Host "Try $i , Setting service $svc StartType to Manual on $pc ..."
                $DisabledSvcs+=$svc
                Set-Service -ComputerName $pc -Name $svc -StartupType Manual -ErrorAction SilentlyContinue}
            Write-Host "Try $i / 10 , Starting $svc Service on $pc ..."
            $commandz="sc "+$pc +" Start "+$svc
            & cmd.exe /c $commandz | Out-Null
            $i++
            sleep 3}
            if($i -ge 10){break}
        }
    if($i -ge 10){Write-Host "Could NOT start service $svc, Skipping Computer $pc" -ForegroundColor Red}else{
    New-Item -Path $RemotePcStagingDir -ItemType Directory -Force -ErrorAction SilentlyContinue
    if($RemoteWinDir=Invoke-Command -ComputerName $pc -ScriptBlock {return $env:windir} -ErrorAction SilentlyContinue){
    foreach($FontFile in (Get-ChildItem -file -path $FontDir)){
        if(-not(Test-Path "$pc$($RemoteWinDir.replace(':','$'))Fonts$FontFile")){
            Copy-Item "$FontDir$FontFile" -Destination $RemotePcStagingDir -Force
            Invoke-Command -ComputerName $pc -ScriptBlock {
           $filePath="$using:PcStagingDir$using:FontFile"
           $fontRegistryPath = "HKLM:SOFTWAREMicrosoftWindows NTCurrentVersionFonts"
           $fontsFolderPath = "$($env:windir)fonts"
        # Create hashtable containing valid font file extensions and text to append to Registry entry name.
        $hashFontFileTypes = @{}
        $hashFontFileTypes.Add(".fon", "")
        $hashFontFileTypes.Add(".fnt", "")
        $hashFontFileTypes.Add(".ttf", " (TrueType)")
        $hashFontFileTypes.Add(".ttc", " (TrueType)")
        $hashFontFileTypes.Add(".otf", " (OpenType)")
        try
        {
            [string]$filePath = (Get-Item $filePath).FullName
            [string]$fileDir  = split-path $filePath
            [string]$fileName = split-path $filePath -leaf
            [string]$fileExt = (Get-Item $filePath).extension
            [string]$fileBaseName = $fileName -replace($fileExt ,"")
    
            $shell = new-object -com shell.application
            $myFolder = $shell.Namespace($fileDir)
            $fileobj = $myFolder.Items().Item($fileName)
            $fontName = $myFolder.GetDetailsOf($fileobj,21)
            
            if ($fontName -eq "") { $fontName = $fileBaseName }
    
            copy-item $filePath -destination $fontsFolderPath
    
            $fontFinalPath = Join-Path $fontsFolderPath $fileName
            if (-not($hashFontFileTypes.ContainsKey($fileExt))){Write-Host "File Extension Unsupported";$retVal = 0}
            if ($retVal -eq 0) {
                Write-Host "Font `'$($filePath)`'`' installation failed on $env:computername" -ForegroundColor Red
                Write-Host ""
                1
            }
            
            else
            {
                Set-ItemProperty -path "$($fontRegistryPath)" -name "$($fontName)$($hashFontFileTypes.$fileExt)" -value "$($fileName)" -type STRING
                Write-Host "Font `'$($filePath)`' $fontName $($hashFontFileTypes.$fileExt) installed successfully on $env:computername" -ForegroundColor Green
            }
    
        }
        catch
        {
            Write-Host "An error occured installing `'$($filePath)`' on $env:computername" -ForegroundColor Red
            Write-Host "$($error[0].ToString())" -ForegroundColor Red
            $error.clear()
        }
        }
         }
         Remove-Item "$RemotePcStagingDir$FontFile" -ErrorAction SilentlyContinue
    }
    Write-Host "Done working on Remote Pc $pc" -ForegroundColor Green
    }else{Write-Host "Could not Invoke-Command to Remote Pc $pc" -ForegroundColor Red}
    }
    foreach($DisabledSvc in $DisabledSvcs){
        Write-Host "Setting service $DisabledSvc StartType to Disabled on $pc ..."
        Set-Service -ComputerName $pc -Name $DisabledSvc -StartupType Disabled -ErrorAction SilentlyContinue
        $commandz="sc "+$pc +" Stop "+$DisabledSvc
        & cmd.exe /c $commandz | Out-Null
    }
    }Else{Write-Host "No Connection to WinRM service on Remote Pc $pc" -ForegroundColor Red}
    }

     

  • Load More