Repository: PowerShell/PowerShell
File: src/Microsoft.PowerShell.Commands.Utility/commands/utility/WriteProgressCmdlet.cs (C#)

Code before:
        /// </summary>
        [Parameter]
        public int SourceId { get; set; }

        /// <summary>
        /// Writes a ProgressRecord created from the parameters.
        /// </summary>
        protected override
        void
        ProcessRecord()
        {
            ProgressRecord pr;
            if (string.IsNullOrEmpty(Activity))
            {
                if (!Completed)
                {
                    ThrowTerminatingError(new ErrorRecord(
                    new ArgumentException("Missing value for mandatory parameter.", nameof(Activity)),
                    "MissingActivity",
                    ErrorCategory.InvalidArgument,
                    Activity));
                    return;
                }
                else
                {
                    pr = new(Id);
                }
            }
            else
            {
                pr = new(Id, Activity, Status);
            }

            pr.ParentActivityId = ParentId;
            pr.PercentComplete = PercentComplete;
            pr.SecondsRemaining = SecondsRemaining;
            pr.CurrentOperation = CurrentOperation;
            pr.RecordType = this.Completed ? ProgressRecordType.Completed : ProgressRecordType.Processing;

            WriteProgress(SourceId, pr);
        }

        private bool _completed;

        private const string HelpMessageBaseName = "WriteProgressResourceStrings";
    }
}


Review comment (suggestion):
it looks like it might be possible to lose the status if the user provided one. the scenario I'm thinking of is something like this:
`1..10|%{ write-progress -id 1 -status "$($_ * 10)% complete"; start-sleep 1 }`

Code after:
        /// </summary>
        [Parameter]
        public int SourceId { get; set; }

        /// <summary>
        /// Writes a ProgressRecord created from the parameters.
        /// </summary>
        protected override
        void
        ProcessRecord()
        {
            ProgressRecord pr;
            if (string.IsNullOrEmpty(Activity))
            {
                if (!Completed)
                {
                    ThrowTerminatingError(new ErrorRecord(
                    new ArgumentException("Missing value for mandatory parameter.", nameof(Activity)),
                    "MissingActivity",
                    ErrorCategory.InvalidArgument,
                    Activity));
                    return;
                }
                else
                {
                    pr = new(Id);
                    pr.StatusDescription = Status;
                }
            }
            else
            {
                pr = new(Id, Activity, Status);
            }

            pr.ParentActivityId = ParentId;
            pr.PercentComplete = PercentComplete;
            pr.SecondsRemaining = SecondsRemaining;
            pr.CurrentOperation = CurrentOperation;
            pr.RecordType = this.Completed ? ProgressRecordType.Completed : ProgressRecordType.Processing;

            WriteProgress(SourceId, pr);
        }

        private bool _completed;

        private const string HelpMessageBaseName = "WriteProgressResourceStrings";
    }
}