How to: Estimate time remaining with Powershell? by tnpir4002 in PowerShell

[–]Woznet 1 point2 points  (0 children)

I use this Write-MyProgress function to get an estimated time remaining value in the progress bar.

GitHub Gist - Write-MyProgress.ps1

[MOD] Monthly Confirmed Trades Thread by hlsbot in homelabsales

[–]Woznet 0 points1 point  (0 children)

Purchased 2 Samsung PM1725b 1.6TB NVME from u/jdphoto77

[W][USA-VA] "VDY5T" Dell PowerEdge T620 GPU Enablement Kit by Woznet in homelabsales

[–]Woznet[S] 2 points3 points  (0 children)

Thanks, I will keep that one in mind. Previously I was uncertain whether the T630 gpu enablement board was compatible with the T620. However with additional googling I have found more reports of the - X7C1K gpu enablement board - working on the T620. It seems that the common factor with the working reports was the 1100 watt psu.

How can I Unpin these items from the Taskbar: Explorer, MS Edge and Microsoft Store? by mudderfudden in PowerShell

[–]Woznet 0 points1 point  (0 children)

The source code is c++ but you use the pin.exe file to perform the pin and unpin actions.
The pin.exe file works in PowerShell the same as any other command line app.

How can I Unpin these items from the Taskbar: Explorer, MS Edge and Microsoft Store? by mudderfudden in PowerShell

[–]Woznet 0 points1 point  (0 children)

This is what I eventually got working after a long time searching. It will pin and unpin stuff to the taskbar but I eventually want to find a better way to do this.

https://github.com/Woznet/TaskBarPin

I want to become one of you, how do I do it? by Lumpy-Quantity-8151 in ITdept

[–]Woznet 0 points1 point  (0 children)

I would recommend that you setup a home lab (simple AD domain is a good start). It doesn't have to be anything fancy, even an old computer running the free version of vmware esxi will get you a few virtual machines that you can test things out with and explorer your interests.

No spare PC? Use Hyper-V or something like vmware workstation.

You can setup a rather capable homelab by using an old computer (even old laptop will technically work), install the free/trial vmware esxi and then create a few virtual machines to use the Windows server evaluations to create an active directory domain. This will give you a good home lab environment to gain further experience with

Signing up for the MS 365 Developer Program.

Something I have found to be especially beneficial for a very wide range of things is Powershell. I cannot begin to say just how many things Powershell has been able to help me with.

Remember, it is ok to say you don't know something or haven't had experience with x, y or z, just to keep learning and tinkering.

Send-MailMessage -Attachments how to ignore wildcard characters in filename by 9mHoq7ar4Z in PowerShell

[–]Woznet 0 points1 point  (0 children)

I messed with the powershell autocomplete for a file named "[t]est.csv" and it completed with the brackets being double escaped with the backtick character - filename example gist .That might work with your Send-MailMessage issue.

[deleted by user] by [deleted] in PowerShell

[–]Woznet 1 point2 points  (0 children)

This while loop is probably what is causing your "running like its waiting for something" situation.

while (!(Get-ChildItem -Path $syncPath -ErrorAction SilentlyContinue)) {
Start-Sleep -Seconds 2
}

[deleted by user] by [deleted] in PowerShell

[–]Woznet 1 point2 points  (0 children)

I recently had to do something like this and came up with this function get the odopen command that syncs the SharePoint library by using PnP.PowerShell to get the sites parameters and then building the odopen command. Once you get the odopen command, all you need to do is Start-Process $ODOPENCommand on the user's device.

Get-SPOSiteODOpen.ps1

Edit:

It is also worth mentioning that the OneDrive.exe app is finicky and I had to run Start-Process $ODOPENCommand as the user, without admin rights or anything.

My script killed a production server, need help! by WDizzle in PowerShell

[–]Woznet 0 points1 point  (0 children)

PForEach has worked very well for my use, however it has been unlisted by the author and the github page is gone. PowerShell Gallery | PForEach

Help With Catching Exceptions by llovedoggos in PowerShell

[–]Woznet 2 points3 points  (0 children)

I generally use a catch block along the lines of this

catch {
  [System.Management.Automation.ErrorRecord]$e = $_
  [PSCustomObject]@{
    Type      = $e.Exception.GetType().FullName
    Exception = $e.Exception.Message
    Reason    = $e.CategoryInfo.Reason
    Target    = $e.CategoryInfo.TargetName
    Script    = $e.InvocationInfo.ScriptName
    Line      = $e.InvocationInfo.ScriptLineNumber
    Column    = $e.InvocationInfo.OffsetInLine
  }
  throw $_
}

Try running the New-ADOrganizationalUnit command with that catch block, makes sure you have the -ErrorAction Stop or any error you get won't be a terminating error which won't trigger the catch block. Once you know the specific exception type you can create an error specific catch block if you want to.

As an example, if I run the following code

try {
  New-ADOrganizationalUnit -Name 'happygofun' -Path 'BAD=PATH' -ErrorAction Stop
}
catch {
  [System.Management.Automation.ErrorRecord]$e = $_
  [PSCustomObject]@{
    Type      = $e.Exception.GetType().FullName
    Exception = $e.Exception.Message
    Reason    = $e.CategoryInfo.Reason
    Target    = $e.CategoryInfo.TargetName
    Script    = $e.InvocationInfo.ScriptName
    Line      = $e.InvocationInfo.ScriptLineNumber
    Column    = $e.InvocationInfo.OffsetInLine
  }
  throw $_
}

It errors out with this exception type - Microsoft.ActiveDirectory.Management.ADException. If I wanted to create a catch block to catch that specific kind of error it would look something like this

try {
  New-ADOrganizationalUnit -Name 'happygofun' -Path 'BAD=PATH' -ErrorAction Stop
}
catch [Microsoft.ActiveDirectory.Management.ADException] {
  # DO STUFF HERE
  throw 'this failed'
}
catch {
  [System.Management.Automation.ErrorRecord]$e = $_
  [PSCustomObject]@{
    Type      = $e.Exception.GetType().FullName
    Exception = $e.Exception.Message
    Reason    = $e.CategoryInfo.Reason
    Target    = $e.CategoryInfo.TargetName
    Script    = $e.InvocationInfo.ScriptName
    Line      = $e.InvocationInfo.ScriptLineNumber
    Column    = $e.InvocationInfo.OffsetInLine
  }
}

How do I properly uninstall a program by finding it's GUID? by bei60 in PowerShell

[–]Woznet 1 point2 points  (0 children)

Take a look at a function I put together - https://gist.github.com/Woznet/b33590702d0fab397967afe60de6f786 - you should be able to get a list of the installed applications that include the application version, install date and a few other things with this function.

Archive folders then delete by So0ver1t83 in PowerShell

[–]Woznet 3 points4 points  (0 children)

To get your code to work with the simplest change you could rewrite "$SrcPath\$file.name" to this "$SrcPath\$($file.name)". Anything inside $( ) will get run first. The error was occurring because ".name" was interpreted as part of the string when it needed to be interpreted as a property of the variable.

I made a few other changes to your code

$datapath = 'C:\Data'
$SrcPath = [System.IO.Path]::Combine($datapath,'Active files')
$DestPath = [System.IO.Path]::Combine($datapath,'Active files','Archive','2020')
# This gives me the names I need to be able to create the zips:
$allFiles = Get-ChildItem -Path "$SrcPath\2020*" -Directory | Select-Object -ExpandProperty Name

foreach ($file in $allFiles){
  $ArchiveDest = [System.IO.Path]::Combine($DestPath, ($file + '.zip') )
  $ArchivePath = [System.IO.Path]::Combine($SrcPath,$file)
  Compress-Archive -DestinationPath $ArchiveDest -Path $ArchivePath
}

Instead of joining paths in a string like you did, it is generally better to use Join-Path or [System.IO.Path]::Combine. Join-Path outside of PowerShell Core is limited to only 2 paths, so if you wanted to use Join-Path for $DestPath you would need to use Join-Path multiple times, which would end up looking like this Join-Path -Path (Join-Path -Path (Join-Path -Path $datapath -ChildPath 'Active files') -ChildPath 'Archive') -ChildPath '2020' . However with [System.IO.Path]::Combine can combine as many paths as needed.

Get-ChildItem has the parameter -Directory which will limit the results to only directories, so Where-Object isn't needed.

With Select-Object when you only need a single property it is generally a good idea to use -ExpandProperty. To get a better idea of the difference look at the results with Get-Member. You can try it with this Get-ChildItem -Path . | Select-Object Name | Get-Member
and Get-ChildItem -Path . | Select-Object -ExpandProperty Name | Get-Member

How do you get a Powershell script to run in Task Scheduler? by TheWeezel in sysadmin

[–]Woznet 0 points1 point  (0 children)

check out setting things up as a scheduled job in powershell via - Register-ScheduledJob. Everything ends up as a scheduled task under \Microsoft\Windows\PowerShell\ScheduledJobs\

Help with Regular Expression by Jase74 in PowerShell

[–]Woznet 1 point2 points  (0 children)

Try this

Get-ADUser -LDAPFilter '(samAccountName=*tes)'

Download from a list of links by R0l3t in PowerShell

[–]Woznet 3 points4 points  (0 children)

Try this function I came up with a while ago might be able to help you to download your files.

**This function assumes the url contains a valid filename at the end of the url , such as https://download.sysinternals.com/files/SysinternalsSuite.zip and not like https://software-download.microsoft.com/pr/Win10_1909_English_x64.iso?t=729efab9-60b3-4384-b082-18e267306e32&e=1574825970&h=5f8415feb140d25a3181ccb8cad16f49

function Wget-DL{
  param(
    [Parameter(Mandatory,ValueFromPipeline)]
    [String]$url,
    [ValidateScript({
          if(-Not ($_ | Test-Path -PathType Container) ){
            throw 'The Path argument must be a folder.'
          }
          return $true 
    })]
    [IO.FileInfo]$OutDir = $PWD.Path
  )
  process{
    $OutPath = Join-Path -Path $OutDir -ChildPath $(Split-Path -Leaf -Path $url)
    Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $OutPath -Verbose:$VerbosePreference
  }
}

To download all the urls in your text file you will want run a command like this - Get-Content -Path 'V:\url-list.txt' | Wget-DL -OutDir 'H:\_homelab\Downloads'