\(\left(x^2-x+2\right)\left(x-1\right)-x^2\left(x-1\right)^2+\left(2x+1\right)\left(x-1\right)^3\)
\(=\left(x-1\right)\left[x^2-x+2-x^2\left(x-1\right)+\left(2x+1\right)\left(x^2-2x+1\right)\right]\)
\(=\left(x-1\right)\left(x^2-x+2-x^3+x^2+2x^3-4x^2+2x+x^2-2x+1\right)\)
\(=\left(x-1\right)\left(x^3-x^2-x+3\right)\)